delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/02/14/23:17:48

X-Spam-Check-By: sourceware.org
Date: Thu, 15 Feb 2007 07:17:33 +0300
From: Andrew Makhorin <mao AT gnu DOT org>
X-Mailer: The Bat! (v2.0 Beta/1) Personal
Reply-To: Andrew Makhorin <mao AT gnu DOT org>
Message-ID: <13811889795.20070215071733@gnu.org>
To: cygwin AT cygwin DOT com
CC: Christopher Faylor <cgf-use-the-mailinglist-please AT cygwin DOT com>
Subject: Re: strange bug in gettimeofday function
MIME-Version: 1.0
X-detected-kernel: Error: This connection is not (no longer?) in the cache.
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

>>>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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019