man*_*ans 5 c c++ memory-leaks
我有这个代码:
int tim=10000; // some random number
tm *now=localtime(&tim);
printf("Date is %d/%02d/%02d\n", now->tm_year+1900, now->tm_mon+1, now->tm_mday);
printf("Time is %02d:%02d\n", now->tm_hour, now->tm_min);
Run Code Online (Sandbox Code Playgroud)
我想知道它是否有内存泄漏的原因是localtime返回一个结构的指针,这意味着它分配内存.但没有人释放它.
这段代码有没有内存泄漏?
你没有(也不应该)必须显式释放任何东西,因为它localtime返回一个指向静态对象的指针.
C标准说:
(C11,7.27.3时间转换函数p1)"除了strftime函数之外,这些函数都返回指向两种静态对象之一的指针:一个分解的时间结构或一个char数组."
从POSIX.1-2008文档中,
asctime(),ctime(),gmtime()和localtime()函数应返回两个静态对象之一的值:一个分解的时间结构和一个char类型的数组.执行任何功能都可能会覆盖任何其他功能在这些对象中返回的信息.
| 归档时间: |
|
| 查看次数: |
138 次 |
| 最近记录: |