本文介绍了如何在C中分配本地时间的结果结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩C中的 time.h 文件,它可以帮助我们处理时间/日期函数。

我碰到过:

  struct tm * _Cdecl localtime(const time_t * __ timer); 

...这似乎会返回一个指向tm struct的指针。我发现地址返回主要用于返回新的内存分配。



如果是这样,上述返回结果是如何工作的(返回地址 struct tm )。返回的对象是在什么地方定义的?



谢谢

解决方案

localtime (和一些其他函数)返回的指针实际上是指向静态分配的内存的指针。 所以你不需要释放。此外,您不应该将其释放。

编辑:添加注释中提到的几件事。



这个共享数据结构的直接结果是 localtime 和类似的函数不是线程安全的。线程安全解决方案因不同的平台而异。 和。


I was playing with the time.h file in C that helps us with time/day functions.

I came across:

struct tm * _Cdecl localtime(const time_t *__timer);

...which seems to return a pointer to tm struct. I have found that return by address is mostly used to return new memory allocations.

If this is so, how does the above return actually work (the return address of a struct tm). Is the returned object defined somewhere?

Thanks

解决方案

The pointer returned by localtime (and some other functions) are actually pointers to statically allocated memory. So you do not need to freed. Furthermore, you should not free it.

http://www.cplusplus.com/reference/clibrary/ctime/localtime/

EDIT : Appending a few things mentioned in the comments.

A direct result of this shared data-structure is that localtime and similar functions are not thread-safe. The thread-safe solution varies with different platforms. localtime_r for POSIX and localtime_s for MSVC.

这篇关于如何在C中分配本地时间的结果结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:03