Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C error: storage size isn’t known

Tags:

c

I wanna run Thimoty's Rolfe implementation of mergesort in MPI, but to get it working I need to compile this wallClock.c file that he provides.

#include <time.h>


double wallClock(void)
{
   struct timeval tv;
   double current;

   gettimeofday(&tv, NULL);   // Omit the timezone struct
   current = tv.tv_sec + 1.0e-06 * tv.tv_usec;

   return current;
}

When compiling I get the following error:

wallClock.c:12: error: storage size of ‘tv’ isn’t known

How can I fix this?

btw, I changed his #include <sys/time.h> to #include <time.h>

it generated the following errors

wallClock.c:15: error: ‘NULL’ undeclared (first use in this function)
wallClock.c:15: error: (Each undeclared identifier is reported only once
wallClock.c:15: error: for each function it appears in.)

I tried including stdlib.h (to fix the NULL undeclared one) and got even more obscure errors.

like image 292
andandandand Avatar asked Dec 23 '11 20:12

andandandand


2 Answers

btw, I changed his #include <sys/time.h> to #include <time.h>

Wrong move. The standard says:

The <sys/time.h> header shall define the timeval structure, which shall include at least the following members:

time_t         tv_sec      Seconds. 
suseconds_t    tv_usec     Microsecods.

EDIT

I took the time and compiled your code, after changing time.h to sys/time.h and adding stdlib.h. I got no errors. I'm using gcc version 4.6.2 20111125 on a fairly recent Linux distro.

like image 73
cnicutar Avatar answered Sep 28 '22 13:09

cnicutar


Ideally, you should be using the modern POSIX clock_gettime (with struct timespec) instead of the now-deprecated (since 2008) gettimeofday. clock_gettime / timespec is also defined in <time.h>, so you already have everything almost right.

like image 32
jørgensen Avatar answered Sep 28 '22 11:09

jørgensen