int main() {
time_t *ptr;
struct tm *dates;
time(ptr);
gmtime_r(ptr, dates);
size_t a = 20; //<-- works with int
return 0;
}
它因段错误(核心转储)
错误而失败。当我使用 int
而不是 size_t
时,一切正常。当我将 gmtime_r
更改为非线程安全 gmtime
时,它也可以工作,尽管我必须添加 gmtime
将分配给的指针声明。 Declaration of gmtime_r
.
gcc版本为5.4.0,使用gcc -Wall -o a test.c
编译,64位ubuntu。
最佳答案
C 中的指针就是这样,它们标识数据可能存在的位置(或可能不存在,在这种情况下,未定义的行为是规则)。
int main() {
time_t ptr; // Actual storage for time_t
struct tm dates; // Actual storage for struct tm
time(&ptr); // Pointer to a time_t
gmtime_r(&ptr, &dates); // Pointer to a time_t, pointer to a
// struct tm
size_t a = 20; //<-- works with int
return 0;
}
关于c - gmtime_r 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42518735/