This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: strange bug in gettimeofday function


>>>I detected a strange bug in the standard function gettimeofday.
>>>It *sometimes* reports the time which being expressed as the integer
>>>number of milliseconds is *less* than the time obtained *earlier* with
>>>the same function.

>>If you mean that you call gettimeofday twice and you get different 
>>values at each call, with a difference of about 100 ms, then this is all 
>>right.  Read on...

> As the author of the function in question, I would still like to get
> a test case.

Looks like I do not understand something elementary :(

If I compile a test program (see below) with `gcc test.c', it works
fine. However, if I compile it with `gcc -O2 test.c', it fails.
Could anyone please explain me what is hapenning?

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

double get_time(void)
{     struct timeval tv;
      gettimeofday(&tv, NULL);
      return (double)tv.tv_sec + 1e-6 * (double)tv.tv_usec;
}

int main(void)
{       double t0 = get_time(), t1 = get_time();
        if (t0 > t1)
        {       int *t;
                printf("sizeof(double) = %d\n", sizeof(double));
                t = (int *)&t0;
                printf("t0 = %08X %08X\n", t[1], t[0]);
                t = (int *)&t1;
                printf("t1 = %08X %08X\n", t[1], t[0]);
                printf("t1 - t0 = %.20g \n", t1 - t0);
                exit(EXIT_FAILURE);
        }
        return 0;
}

In case of failure the output is the following:

sizeof(double) = 8
t0 = 41D174F7 4F2AE148
t1 = 41D174F7 4F2AE148
t1 - t0 = 0


Thank you,

Andrew Makhorin


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]