|
Home > Archive > Unix Programming > April 2004 > Strange bug with C time() function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Strange bug with C time() function
|
|
| Ben Leane 2004-04-20, 8:35 am |
| I've come across a problem when using time() in my C code.
The following code has been tested in isolation, and when run the
second print of startTime will be equal to endTime, and not to the
first print.
time_t startTime = time(NULL);
sleep(10);
printf("Start time is %s", ctime(&startTime));
time_t endTime = time(NULL);
printf("Start time is %s\nEnd time is %s", ctime(&startTime),
ctime(&endTime));
It seems the second time() call is somehow reseting the startTime
variable to the current time. The code looks right, so has anyone
heard of this happening?
I'm also finding that clock() is always reporting 0 elapsed time, yet
neither function is returning an error (-1). Is this an indication
that something to do with system time is not configured properly on my
system or something?
-Ben
| |
|
| "Ben Leane" <soundwave6@hotmail.com> wrote in message
news:3b8ecb85.0404200353.5b528093@posting.google.com...
> I've come across a problem when using time() in my C code.
>
> The following code has been tested in isolation, and when run the
> second print of startTime will be equal to endTime, and not to the
> first print.
>
> time_t startTime = time(NULL);
> sleep(10);
> printf("Start time is %s", ctime(&startTime));
> time_t endTime = time(NULL);
> printf("Start time is %s\nEnd time is %s", ctime(&startTime),
> ctime(&endTime));
>
> It seems the second time() call is somehow reseting the startTime
> variable to the current time. The code looks right, so has anyone
> heard of this happening?
No, it's not the calls to time() that are the problem. Clue: try changing
the second printf() to print the end time first.
I think it's inevitable that the two printed times will be the same, and
that either time may be printed. Perhaps this will make the problem more
obvious (read your compiler's documentation for ctime()):
const char *startString;
const char *endString;
time_t startTime = time(NULL);
sleep(10); /* non-standard function, but not relevant to your problem */
printf("Start time is %s", ctime(&startTime));
time_t endTime = time(NULL);
startString = ctime(&startTime);
endString = ctime(&endTime)
printf("Start time is %s\nEnd time is %s", startString, endString);
If you still don't get it, try adding:
printf("startString = %p, endString = %p\n", (void *)startString,
(void *)endString);
> I'm also finding that clock() is always reporting 0 elapsed time, yet
> neither function is returning an error (-1). Is this an indication
> that something to do with system time is not configured properly on my
> system or something?
Probably not, just that the environment (OS, compiler and standard library)
does not support it.
| |
| Maurizio Loreti 2004-04-20, 12:35 pm |
| soundwave6@hotmail.com (Ben Leane) writes:
> I've come across a problem when using time() in my C code.
> ...
> It seems the second time() call is somehow reseting the startTime
> variable to the current time. The code looks right, so has anyone
> heard of this happening?
From "man ctime":
NOTES
The four functions asctime(), ctime(), gmtime() and local-
time() return a pointer to static data
There is no sequence point between the invocation of your ctime()
calls in the last printout.
--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
| |
| Nils O. Selåsdal 2004-04-20, 12:35 pm |
| On Tue, 20 Apr 2004 04:53:36 -0700, Ben Leane wrote:
> I've come across a problem when using time() in my C code.
>
> The following code has been tested in isolation, and when run the
> second print of startTime will be equal to endTime, and not to the
> first print.
>
> time_t startTime = time(NULL);
> sleep(10);
> printf("Start time is %s", ctime(&startTime));
> time_t endTime = time(NULL);
> printf("Start time is %s\nEnd time is %s", ctime(&startTime),
> ctime(&endTime));
>
> It seems the second time() call is somehow reseting the startTime
> variable to the current time. The code looks right, so has anyone
> heard of this happening?
That's not the case. If your system has ctime_r , use that instead
of ctime.
--
Nils Olav Selåsdal
System Engineer
w w w . u t e l s y s t e m s . c o m
|
|
|
|
|