Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ SMAUG
➜ SMAUG coding
➜ Strange bug with Ctime
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Dace K
Canada (169 posts) Bio
|
Date
| Sun 12 Mar 2006 07:24 PM (UTC) Amended on Sun 12 Mar 2006 07:25 PM (UTC) by Dace K
|
Message
| For some reason, ctime won't show any time but the character's logon time in score, but updates just fine on the do_time command.
void do_time( CHAR_DATA *ch, char *argument )
{
extern char str_boot_time[];
extern char reboot_time[];
char *suf;
int day;
day = time_info.day + 1;
if ( day > 4 && day < 20 ) suf = "th";
else if ( day % 10 == 1 ) suf = "st";
else if ( day % 10 == 2 ) suf = "nd";
else if ( day % 10 == 3 ) suf = "rd";
else suf = "th";
set_char_color( AT_YELLOW, ch );
ch_printf( ch,
"It is %d o'clock %s, Day of %s, %d%s the Month of %s.\n\r"
"The mud started up at: %s\r"
"The system time (E.S.T.): %s\r"
"Next Reboot is set for: %s\r",
(time_info.hour % 12 == 0) ? 12 : time_info.hour % 12,
time_info.hour >= 12 ? "pm" : "am",
day_name[day % 7],
day, suf,
month_name[time_info.month],
str_boot_time,
(char *) ctime( ¤t_time ),
reboot_time
);
return;
}
The mud started up at: Sun Mar 12 16:19:47 2006
The system time (E.S.T.): Sun Mar 12 16:19:57 2006
Next Reboot is set for: Tue Mar 14 06:00:00 2006
void do_score(CHAR_DATA * ch, char *argument)
{
char buf[MAX_STRING_LENGTH];
/*AFFECT_DATA *paf;*/
if (IS_NPC(ch))
{
do_oldscore(ch, argument);
return;
}
set_pager_color(AT_SCORE, ch);
pager_printf(ch, "\n\r&G%s&c%s&g.\n\r", ch->name, ch->pcdata->title);
if ( get_trust( ch ) != ch->level )
pager_printf( ch, "You are trusted at level %d.\n\r", get_trust( ch ) );
send_to_pager("&W----------------------------------------------------------------------------\n\r", ch);
pager_printf(ch, "&GLogin&g: &w%s&GCurrent Time&g: &w%s\r",
ctime(&(ch->logon)), ctime( ¤t_time ));
pager_printf(ch, "&GPlayed&g: &w%d hours &GSaved&g: &w%s\r",
(get_age(ch) - 17) * 2), (ch->save_time ? ctime(&(ch->save_time)) : "no save this session\n");
Login: Sun Mar 12 16:19:47 2006
Current Time: Sun Mar 12 16:19:47 2006
Played: 780 hours Saved: Sun Mar 12 16:19:47 2006
Any thoughts? |
ASJ Games - .Dimension 2, Resident Evil, and snippets - oh my!
http://asj.mudmagic.com
Drop by the area archives and find something for your mud. http://areaarchives.servegame.com | Top |
|
Posted by
| Dace K
Canada (169 posts) Bio
|
Date
| Reply #1 on Sun 12 Mar 2006 07:40 PM (UTC) |
Message
| After some testing, I've found that ctime will only return the first value it's used for in a function, regardless of what it's being called for. (e.g. swapping the values for logon time and current time will result in current time being used for the rest of the values in score). |
ASJ Games - .Dimension 2, Resident Evil, and snippets - oh my!
http://asj.mudmagic.com
Drop by the area archives and find something for your mud. http://areaarchives.servegame.com | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #2 on Sun 12 Mar 2006 08:28 PM (UTC) |
Message
| That's not quite the problem. If you look at how ctime is implemented, it uses a static buffer and returns a pointer to that.
So, if you do:
printf("%s %s" ctime(...), ctime(...));
it will evaluate the first ctime, return a pointer to the static buffer, then it will evaluate the second ctime, and return a pointer to the same static buffer. In the end, you have two pointers to the same buffer.
As a technical note, I'm not sure if the function call arguments are evaluated first-to-last or last-to-first. In your case it seems to be last-to-first, because the first value is the one retained. I'm not sure if the standard defines this one way or the other, or if it's an implementation detail.
In any case, the solution is:
char buf[MAX_INPUT_LENGTH];
strcpy(buf, ctime(...));
printf("%s %s", buf, ctime(...));
That way, you're only calling ctime once in the expression, after saving its first result. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Dace K
Canada (169 posts) Bio
|
Date
| Reply #3 on Mon 13 Mar 2006 05:48 PM (UTC) Amended on Mon 13 Mar 2006 05:50 PM (UTC) by Dace K
|
Message
| Thanks, worked a charm. I couldn't find ctime in the code at all :/
Simply breaking up the ctime calls into seperate lines also worked :) |
ASJ Games - .Dimension 2, Resident Evil, and snippets - oh my!
http://asj.mudmagic.com
Drop by the area archives and find something for your mud. http://areaarchives.servegame.com | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #4 on Mon 13 Mar 2006 07:07 PM (UTC) |
Message
| Sorry -- it's actually not a SMAUG function, it's a C-library function.
From the 'man ctime' page:
Quote: NOTES
The four functions asctime(), ctime(), gmtime() and localtime() return a pointer to static data and
hence are not thread-safe. Thread-safe versions asctime_r(), ctime_r(), gmtime_r() and local-
time_r() are specified by SUSv2, and available since libc 5.2.5.
ctime_r is the same as ctime except that it takes a second parameter, which is the buffer in which data is stored. It then returns a pointer to that buffer.
So you could do:char buf1[MAX_INPUT_LENGTH];
char buf2[MAX_INPUT_LENGTH];
printf("%s %s", ctime_r(..., buf1), ctime_r(..., buf2));
Don't you just love little tricks like this. :-) I ran into this some time ago with a function in SMAUG that converted by using a static buffer, and returning pointers to that.
All this is solved so easily in C++ by simply returning a std::string. The problem in C is that you want to return a non-allocated string, for convenience, so that the caller doesn't have to keep freeing these strings. But as soon as you do that, you only have this one static buffer lying around. And then you get confusing cases like this one you ran into.
In C++ you just return a std::string which manages its own memory and as soon as it leaves the scope, everything is happy and the memory is freed automatically. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Reply #5 on Tue 14 Mar 2006 03:32 AM (UTC) |
Message
| Hey, that's a pretty sneaky use of the thread-safe functions. I'm not sure I would have thought to even try that myself :)
I think we've all run into the "don't use 2 static buffers in one call" problem once in our lives. Damn confusing when you don't realize why it's happening. | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
18,051 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top