ctime() error...implementation maybe?

This is a discussion on ctime() error...implementation maybe? within the C++ Programming forums, part of the General Programming Boards category; hello, hopefully someone out there can help me with this. I'm using ctime to print a time_t value in a ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    25

    ctime() error...implementation maybe?

    hello,

    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.

    Code:
    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
    Posts
    5,789
    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 string....so 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
    Posts
    25
    thanks for the help, guys...

    -cheers
    [E]

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, 02:51 PM
  3. Stop ctime from breaking line?
    By trenzterra in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2005, 12:09 PM
  4. ctime library
    By silent_eyes in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2005, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21