Thread: ctime() error...implementation maybe?

  1. #1
    Registered User
    Join Date
    Jun 2002

    ctime() error...implementation maybe?


    hopefully someone out there can help me with this. I'm using ctime to print a time_t value in a more friendly format, but when ctime is called more than once with different values, it prints the same time. here's a small piece of the code, and the output.

    cout << ctime (&(statBuff->st_atime)) 
         << ctime (&(statBuff->st_mtime)) 
         << ctime (&(statBuff->st_ctime)) << endl;
    output example:

    Thu Sep 26 13:38:54 2002
    Thu Sep 26 13:38:54 2002
    Thu Sep 26 13:38:54 2002

    More info....statBuff is a 'struct stat' type, and yes, i did check the members st_atime, st_mtime, st_ctime. they all hold different values so ctime should print a different string for each one.

    I really don't see what i'm doing wrong. i'm really thinking it has something to do with the way ctime is implemented. Do anyone know for sure?

    gcc 2.96 (Mandrake's version...I don't have a choice on this one)...

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Its the same on VC++....but its more to do with the way the std implements this function than how the vendor does....

    Ok.....So you call ctime....and get back a pointer to a string......but you dont make room for that string....that string is owned by the c runtime......So in [E]'s code, the compiler first calls ctime (&(statBuff->st_ctime))...then in the EAX register (assuming 0x86 type chip..) holds a pointer to that memory that holds the return the compiler throws that pointer on the stack and makes the other 2 calls to ctime(), eachtime saving the EAX reg on the stack....then it calls the <<operator on cout 3 times, one for each pushed pointer....but that pointer is the same each time!!!!!!, and it points to a string that was set in the last call to ctime()........

    So your code is logically correct, but as the return is always kept in the same buffer, its overwritten each time ctime() is called...therefore 3 calls to cout on that pointer will obviously give the same result each time...

    BTW, its 10PM in the UK, if anyone reads this in the next 30 mins, the new series of League of Gentlemen has begun!!!! Get to the TV and switch to BBC2

  3. #3
    Registered User
    Join Date
    Jun 2002
    thanks for the help, guys...


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes in C++
    By Kool4School in forum C++ Programming
    Replies: 22
    Last Post: 01-21-2009, 09:26 PM
  2. Replies: 5
    Last Post: 09-26-2006, 01:51 PM
  3. Stop ctime from breaking line?
    By trenzterra in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2005, 11:09 AM
  4. ctime library
    By silent_eyes in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2005, 02:06 PM
  5. time precision (ctime), tm struct question
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2003, 01:51 PM