storage size of ‘tzp’ isn’t known - c

So I'm trying to use this piece of code to benchmark:
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
But for some reason I keep getting this error
error: storage size of ‘tzp’ isn’t known
warning: unused variable ‘tzp’
Any help would be appreciated.

You defined _XOPEN_SOURCE=500. According to the X/Open 5, the second argument is of type void* and must be NULL:
int gettimeofday(struct timeval *tp, void *tzp);
[..]
If tzp is not a null pointer, the behaviour is unspecified.
If you want the prototype specified in the linux manual, you need to
#define __USE_BSD
However, if you pass anything other than NULL, it will return an error.

You can just get rid of the 'tzp' variable.
From the gettimeofday man page:
If either tv or tz is NULL, the corresponding structure is not set or returned.
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.
So your code should be something like:
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + t.tv_usec*1e-6;
}

Related

error: dereferencing pointer to incomplete type on stub of gettimeofday

Haven't done much c programming and am running into "error: dereferencing pointer to incomplete type".
Attempting to stub out gettimeofday with call to clock_gettime.
Here is code
#include <time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz)
/* This procedure stubs out call to gettimeofday */
{
struct timespec spec;
// initialize result status to invalid
int result = -1;
// if passed in pointer tv is not NULL
if (tv) {
// retrieve time
result = clock_gettime(CLOCK_REALTIME, &spec);
// if time retreived is valid then
if (result == 0){
tv->tv_sec = spec.tv_sec; // seconds
tv->tv_usec = (spec.tv_nsec / 1.0e3); // Convert nanoseconds to microseconds
}
}
return result;
}
I get the "error dereferencing pointer to incomplete type" on assignment
tv->tv_sec = spec.tv_sec; // seconds
If I compile/link in linux target=i686-pc-linux-gnu but no error intarget=powerpc-xcoff-lynxos178 target environment.
I am #include and that has definition timespec
time.h is different for each target. Thank you for taking time to look at this.
Along with the need for #include <sys/time.h> you are probably seeing a conflict with the prototype for the system's gettimeofday() call. On many systems the second parameter is void *.
The issue for this stub of gettimeofday was we were trying to write the stub to work in 3 different environments. Windows, Linux target and powerpc target - each of which seems to have slight variants in OS headers. What we ended up doing was adding a compiler directive to handle linux vs the other environments where we did not have issues. This is ultimately what we went with - I thought we should have avoided the stub and just called clock_gettime but there were reasons why this way was cheaper.
#include <time.h>
#include <sys/time.h>
#ifdef __linux__
int gettimeofday(struct timeval *__restrict __tv, __timezone_ptr_t __tz)
#else
int gettimeofday(struct timeval * __tv, struct timezone* __tz)
#endif

Discover available clock types at compile time

[Ubuntu 14.04, 3.16.0-34-generic Kernel, GCC 4.8.4, Clang 3.5.0]
I'm writing some elapsed time performance routines for an application I have, and I would like to do it in a cross-platform manner.
I would like to write it in such a way that the right clock type is selected during compile, rather than run-time (which I can do by testing for failures and using fallbacks).
The clock_getres(2) man page states:
On POSIX systems on which these functions are available, the symbol _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0. The symbols _POSIX_MONOTONIC_CLOCK, _POSIX_CPUTIME, _POSIX_THREAD_CPUTIME
indicate that CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID are available. (See also sysconf(3).)
I have included in my code , , and but my conditional compile statements don't recognise the symbol _POSIX_MONOTONIC_CLOCK. It always prints the 'gettimeofday' message. I've tried GCC and Clang but get the same result.
My code below is incomplete (and incorrect) and I would appreciate some assistance on how to do this right.
#include <unistd.h>
#include <features.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}
I'm not using any configure or auto-configure software.
Also, a comment about the relative speeds of CLOCK_MONOTONIC & CLOCK_MONOTONIC_RAW would be nice. I understand the difference and their limitations.
You aren’t declaring the headers and feature-test macros that the man page says you need. This works:
#define _POSIX_C_SOURCE 200809L // <- This was missing.
#define _XOPEN_SOURCE 700 // <- This is optional.
#include <unistd.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
#include <time.h> // <- This was missing.
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}

declare struct timeval time inside a function

I have done some search about problems when using time.h to obtain a random seed initialization. Particularly in my case, I want to place the time outside the main function.
Based on the comments I made some changes. After including twice in the include as and , these errors and warning went off:
too few arguments to function ‘random’
implicit declaration of function ‘srandom’ [-Wimplicit-function-declaration]
too few arguments to function ‘random’
Once I declared the code as:
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include <math.h>
int main(void)
{
struct timeval time;
...
}
It works, however it has to move outside main. But if I change to:
#include <sys/time.h>
struct timeval time;
int main(void)
{
...
}
It gives me the error:
‘time’ redeclared as different kind of symbol
In my case the implementation I am working on is in C not in C++. When I try to move the code to a function in order to receive a different random number every time my function is called, some other errors happen:
double myRandom(double dAverage,double dStddev);
double myRandom(double dAverage,double dStddev){
int iRandom1, iRandom2;
double dRandom1, dRandom2,result;
long int liSampleSize;
iRandom1=(random());
dRandom1=(double)iRandom1 /2147483647;
iRandom2=(random());
dRandom2=(double)iRandom2 /2147483647;
result= dAverage + dStddev * sqrt(-2.0 * log(dRandom1))*cos(6.28318531 * dRandom2);
return(result);
}
int main(void){
...
struct timeval time;
gettimeofday(&time, NULL);
srandom((unsigned int) time.tv_usec);
for (i=0; i<liSampleSize;i++)
{ result=myRandom(dAverage,dStddev);
}
}
Apparently it should be working. Has somebody any idea what is wrong here. All comments are highly appreciated.
Update: Then, taking srandom out of myRandom makes the generated values as expected. So, it worked!
The first error is related to time.h. Including it introduces some instance called time to your global namespace.
The second is most likely because you haven't included stdlib.h.

Any specific reason why localtime throws warning with struct tm* & stat* , in linux ?

I have this simple code (part of a project) :
void displayFileProperties(struct stat* file,char* outputProperties , char * path)
{
struct tm* time;
// code
// code
time = localtime(&file->st_mtim);
// code
}
Where eclipse keeps throwing me a warning :
passing argument 1 of ‘localtime’ from incompatible pointer type [enabled by default] main.c /ex4 line 340 C/C++ Problem
Any idea how to fix this ? thanks
st_mtim is a struct timespec (seconds and nanoseconds). You want st_mtime.
You'll want to use this instead:
time = localtime(&file->st_mtime);
Note the added 'e' at the end. st_mtim is a timespec, with 'e' added it's a time_t (what you need).
Completely changed answer:
SUGGESTIONS:
1) Make sure you #include these headers:
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
2) Cast your pointer to "const"
time = localtime((const time_t *)&file->st_mtime);
3) Post back what happens
=====================================================
ADDITIONAL SUGGESTIONS:
1) Please read these two links:
C stat struct does not have st_ctime field but only st_ctim
http://linux.die.net/man/2/lstat
Since kernel 2.5.48, the stat structure supports nanosecond resolution
for the three file timestamp fields. Glibc exposes the nanosecond
component of each field using names of the form st_atim.tv_nsec if the
_BSD_SOURCE or _SVID_SOURCE feature test macro is defined. These fields are specified in POSIX.1-2008, and, starting with version 2.12,
glibc also exposes these field names if _POSIX_C_SOURCE is defined
with the value 200809L or greater, or _XOPEN_SOURCE is defined with
the value 700 or greater. If none of the aforementioned macros are
defined, then the nanosecond values are exposed with names of the form
st_atimensec. On file systems that do not support subsecond
timestamps, the nanosecond fields are returned with the value 0.
2) Clearly, the makefile (that "works") has a #define that Eclipse doesn't, or vice versa.
Probably either/both _POSIX_C_SOURCE and/or _XOPEN_SOURCE.
Run this command to see what exists in the command line (makefile?) environment:
gcc -dM -E - < /dev/null | less
3) Please post back what you find!
I had the same issue with Eclipse:
Field st_mtime could not be resolved (semantic error)
Fixed the issue in Eclipse by right-clicking the project, choose Index->"Freshen All Files"
#include <malloc.h>
#include <time.h>
#include <stdio.h>
static struct tm* alarmTime(void);
int main(){
printf("Hour :%i\n", alarmTime()->tm_hour);
printf("Minute :%i\n", alarmTime()->tm_min);
return 0;
}
static struct tm* alarmTime(void){
time_t now = time(NULL);
struct tm* ptm;
#ifdef HAVE_LOCALTIME_R
struct tm tmbuf;
ptm = localtime_r(&now, &tmbuf);
#else
ptm = localtime(&now);
#endif
return ptm;
}

Getting problem with timing in C in visual studio 2010

I have a function for getting system time. The function is defined as follows.
int getSystemTime(struct timeval tv, void * tz);{
DWORD milliseconds;
milliseconds = timeGetTime();
tv->tv_sec = milliseconds / 1000;
tv->tv_usec = (milliseconds % 1000) * 1000;
return 0;
}
Precisely following are the problems:
1.error: identifier DWORD is undentified .
2.error: identifier timeGetTime() is undefined.
3.error: identifier suseconds_t is undefined.
I tried to include windef.h where DWORD is defined. But the problem is, I got the error like:
1. error: identifier PCONTEXT is undefined.
The header file for time included is time.h. Here the timeval defined is:
#ifndef _WINSOCK_H
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
Could you please tell me what shall I do to make this function run in windows environment?
[EDIT]
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#ifdef HAVE_MMSYSTEM_H
#include <mmsystem.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
You need to include windows.h at the top of your C file.

Resources