gmtime_r和gmtime_s之间的区别
difference between gmtime_r and gmtime_s
这两个函数有什么区别?我正在使用 MinGW 4.8.0。
我知道gmtime_r
线程安全(但如果从同一线程多次调用则不安全),但我不明白gmtime_s
区别在于gmtime_r(3)
是标准的 SUSv2 函数。在Windows环境中,您能找到的最接近gmtime_r()
的是gmtime_s()
,其参数颠倒了:
-
gmtime_r(const time_t*, struct tm*)
-
gmtime_s(struct tm*, const time_t*)
基本上,它们都将时间值转换为tm
结构。 然后gmtime_r
返回指向此结构的指针(如果失败,则返回NULL
),而gmtime_s
如果成功则返回0
,如果失败则返回errno_t
。
tm
结构具有以下正文,从上面列出的两个文档中可以看出:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
gmtime_r
和localtime_r
是标准的POSIX函数。
它们的主要目的是线程安全(重入)。 基本的 gmtime
和 localtime
函数不是线程安全的或可重入的,因为它们使用单个静态区域来存储结果,因此 gmtime_r
和 localtime_r
会指向结果应存储的位置的指针。
gmtime_s
和localtime_s
由Microsoft引入,现在是C11的一部分,尽管非Microsoft支持有限。(有关进一步讨论,请参阅此处。
它们的主要目的是安全性。 它们是作为Microsoft的安全CRT(安全C运行时)的一部分添加的。 据我了解,线程安全不是Microsoft CRT 中gmtime
和localtime
的问题,因为这些函数的静态输出区域已经按线程分配。 相反,添加了gmtime_s
和localtime_s
来执行安全 CRT 的参数验证。(换句话说,它们检查其参数是否为 NULL,在这种情况下,它们会调用错误处理。
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- C++ - "!pointer"和"pointer == nullptr"的区别?
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 返回递归调用和仅递归调用的区别
- Qt:remove() 和 rmdir() 有什么区别
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1
- 这 4 个 lambda 表达式之间有什么区别?
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- (double) 和 double() 之间的区别
- 将向量作为类>(值)<向量启动和向量<类>[值]有什么区别
- typedef 枚举和枚举类有什么区别?
- &C::c 和 &(C::c) 有什么区别?