c - gmtime_r 的奇怪行为

标签 c gcc time

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/

相关文章:

c - 如何将变量插入 sscanf 字符串?

c - 为什么我不能在 C 中获取 asm 寄存器的值?

angular - 时间格式根据 ionic 5 angular 9 中手机的时间设置而变化

c - 为什么 Google 的 TrueTime API 难以复制?

c - 写入数据时从文本文件中删除换行符

c - 搜索数据并将数据从一个文件写入另一个文件

linux - gdb 似乎影响存储在 $eax 中的函数返回值?

ruby-on-rails - 检查当前时间是否在10之前 :27pm

c - 如何格式化代码以便触发 while 循环 C

linux - gcc 错误 - "error adding symbols: DSO missing from command line"