Localtime是否返回分配的TM*

发布于 2025-02-12 12:06:49 字数 185 浏览 0 评论 0 原文

如您所知,< time.h> 标准标头定义 struct tm 和称为 localtime 的函数。

localtime 做出堆分配吗?
还是在堆栈中分配?
它返回指针,但这可能只是指向堆栈值的指针,对吗?

As you know the <time.h> standard header defines the struct tm and a function called localtime.

Does localtime make a heap allocation?
Or is it allocated in the stack?
It returns a pointer but this could be just a pointer to the stack value, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2025-02-19 12:06:49

C标准(C18)的相关部分具有以下语言:

7.27.3时间转换功能

strftime 功能外,这些函数每个函数均返回指针,向两种类型的静态对象之一:折线时间结构或 char> char 的数组。执行将指针返回这些对象类型之一的任何功能都可以覆盖相同类型的任何对象中的信息,该对象指向的任何一个从任何一个呼叫返回的值指向其任何一个,并且功能是
不需要避免彼此进行数据竞赛。实施的行为应像没有其他库功能一样称呼这些功能。

localtime 返回静态 struct tm 对象的指针,即:一个可能是线程本地线程的全局对象。该对象的内容可以被随后呼叫或其他库函数覆盖。将其称为函数的线程退出后,不应访问它。

该对象不是从堆中分配的,您不得用此指针调用免费

在函数 localtime 返回之后访问它的行为将不确定,因此无法将对象分配给自动存储。

您应该使用 localtime_r ,而不是 localtime ,它将包含在C标准的下一个版本中(C23)(C23):

#include <time.h>
struct tm *localtime_r(const time_t *timer, struct tm *buf);

localtime_r 将指针指向目标对象,您可以定义或分配适合使用。

MSVC可能不支持此功能,但是您可以将其定义为一个简单的宏:

#ifdef _MSC_VER
#define localtime_r(a,b)  localtime_s(a,b)
#endif

The relevant part of the C Standard (C18) has this language:

7.27.3 Time conversion functions

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 and the functions are
not required to avoid data races with each other. The implementation shall behave as if no other library functions call these functions.

localtime returns a pointer to a static struct tm object, ie: a global object that may or may not be thread local. The contents of this object can be overwritten by a subsequent call to this or another library function. It should not be accessed after the thread in which it the function was called has exited.

The object is not allocated from the heap, You must not call free with this pointer.

The object cannot be allocated with automatic storage as accessing it after the function localtime returns would have undefined behavior.

Instead of localtime, you should use localtime_r, a POSIX function that will be included in the next version of the C Standard (C23):

#include <time.h>
struct tm *localtime_r(const time_t *timer, struct tm *buf);

localtime_r takes a pointer to the destination object, which you can define or allocate as appropriate for your usage.

MSVC might not support this function, but you can define it on its target platforms as a simple macro:

#ifdef _MSC_VER
#define localtime_r(a,b)  localtime_s(a,b)
#endif
绝情姑娘 2025-02-19 12:06:49

localtime 将指针返回到全局可变,通常在所有线程之间共享:

返回值:指向静态内部 std :: TM 对象成功的指针,或者否则为null指针。该结构可以在 std :: GMTime std :: localtime std :: CTime 之间共享,并且可以在每个调用上覆盖。

此功能可能不是线程安全。

现代应用程序应使用 localtime_r 而不是 localtime

示例: localtime

localtime returns a pointer to a global variable, it is often shared between all threads:

Return value: pointer to a static internal std::tm object on success, or null pointer otherwise. The structure may be shared between std::gmtime, std::localtime, and std::ctime, and may be overwritten on each invocation.

This function may not be thread-safe.

Modern applications should use localtime_r instead of localtime.

Example: glibc implementation of localtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文