ctime: <ctime> char * ctime ( const time_t * timer ); Convert time_t value to string Converts the time_t object pointed by timer to a C string containing a human-readable version of the corresponding local time and date.
The returned string has the following format:
Www Mmm dd hh:mm:ss yyyy Where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.
The string is followed by a new-line character ('\n') and the terminating null-character.
This function is equivalent to: asctime(localtime(timer)).
Parameters: timer Pointer to a time_t object that contains a calendar time.
Return Value: A C string containing the date and time information in a human-readable format.
The array which holds this string is statically allocated and shared by both the ctime and asctime functions. Each time either one of these functions is called the content of this array is overwritten.
Example /* ctime example */ #include <stdio.h> #include <time.h>
int main () { time_t rawtime;
time ( &rawtime );
printf ( "The current local time is: %s", ctime (&rawtime) );
return 0; }
[解决办法]
楼主啊 !我来回答你吧! The array which holds this string is statically allocated and shared by both the ctime and asctime functions. Each time either one of these functions is called the content of this array is overwritten. 意思是说这个数组所占的内存区域是在静态区域分配的,在程序运行时一直存在。而且块区域是ctime和asctime函数所共享的,每次调用这两个函数都是访问的这同一块区域。就算你第二次、第n次调用也不会开辟新的内存,因为这块内存早就准备好了。只等你调用就把这块内存地址返回给你。所以不会造成内存泄露。
第二个问题看ctime的man page: 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 localtime_r() are spec- ified by SUSv2, and available since libc 5.2.5.