time.h 如何处理内存
How time.h handles memory
抱歉,这可能是个愚蠢的问题,但我在 time.h
的文档中找不到答案。
所以当我打电话时,例如,gmtime
time_t today;
struct tm *info;
time(&today);
info = gmtime(&today);
它returns指向tm结构的指针。我假设它 returns 指向用 malloc 分配的内存的一部分,但是如果我现在为 info
调用 free
- free
returns 错误。那么库 time.h
如何处理内存分配,我应该担心 "freeing" 它吗?
that it returns pointer to a part of memory allocated with malloc, but if I call free for info now
不,gmtime
returns 一个指向静态对象的指针。
来自C99 7.23.3p1:
Except for the strftime function, these functions each return a pointer to one of two types of static objects: a broken-down time structure or an array of char. Execution of any of the functions that return a pointer to one of these object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them. The implementation shall behave as if no other library functions call these functions.
how does library time.h handles memory allocation
它使用分配的内存,静态存储持续时间对整个程序执行有效。
should I be worried about "freeing" it?
没有
抱歉,这可能是个愚蠢的问题,但我在 time.h
的文档中找不到答案。
所以当我打电话时,例如,gmtime
time_t today;
struct tm *info;
time(&today);
info = gmtime(&today);
它returns指向tm结构的指针。我假设它 returns 指向用 malloc 分配的内存的一部分,但是如果我现在为 info
调用 free
- free
returns 错误。那么库 time.h
如何处理内存分配,我应该担心 "freeing" 它吗?
that it returns pointer to a part of memory allocated with malloc, but if I call free for info now
不,gmtime
returns 一个指向静态对象的指针。
来自C99 7.23.3p1:
Except for the strftime function, these functions each return a pointer to one of two types of static objects: a broken-down time structure or an array of char. Execution of any of the functions that return a pointer to one of these object types may overwrite the information in any object of the same type pointed to by the value returned from any previous call to any of them. The implementation shall behave as if no other library functions call these functions.
how does library time.h handles memory allocation
它使用分配的内存,静态存储持续时间对整个程序执行有效。
should I be worried about "freeing" it?
没有