ctime返回null

| 如果将用户类型“ 0”定义为“ 1”,而在MacOS X中将其本身定义为“ 2”,那么以下代码为何输出“ 3”?也许这很愚蠢,但我无法真正理解。
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t time = 0x7FFFFFFFFFFFFFFF;

    printf(\"%lu\\n\"
           \"Time is %s\\n\", sizeof(time_t), ctime(&time));

    return 0;
}
    
已邀请:
时间0x7FFFFFFFFFFFFFFFFF似乎出现在公元292,471,210,647年左右,这无疑导致
ctime
超过了C99保证的26个字符,因此它返回NULL而不是溢出其缓冲区。通常,尽量避免在莫洛克人与伊洛伊人交战之后发生的任何日期。     
在阅读《专家C编程》这本书时,我遇到了Lion 10.7.3中的相同问题-用
t=0xf0c00000000000
ctime(&t)
产生
Wed Mar  1 21:07:12     214739252
,用
t=0xf0d00000000000, ctime(&t)
返回空指针(0x0)。 因此它似乎不是t的环绕,而是在
ctime(&t)
中进行了一些测试,如果t太大,它将返回空指针。     
从glibc的实现中,我们读到:   我们限制了可以打印的年份的大小。使用%d     格式说明符使用1900的添加会溢出     数字,并显示负值。对于某些架构,我们     理论上可以使用%ld或Evern更大的整数格式,但是     这意味着输出需要更多空间。这不会是     如果\ asctime_r \'接口被合理定义会出现问题,并且     缓冲区大小将被传递。 运行以下程序以找到您计算机上的确切限制。
#include <limits.h>
#include <stdio.h>
#include <time.h>

/**
 * Find the largest time_t for which ctime returns a non-NULL value
 * using a bsearch between 0 and LONG_MAX.
 **/
static time_t ctime_max() {
    time_t start = 0, end = LONG_MAX, mid;
    while (start < end) {
        mid = start + (end - start) / 2;
        if (ctime(&mid)) {
            /* this mid is ctime-able; try higher */
            start = mid + 1;
        } else {
            /* this mid is not ctime-able; try lower */
            end = mid;
        }
    }
    /* mid is now the lowest number that\'s too high; subtract one */
    return mid - 1;
}

int main() {
    time_t t = ctime_max();
    printf(\"%s\", ctime(&t));
    return 0;
}
对我来说,是12英镑,恰好是该年溢出四个有符号字节之前的第二个字节。     

要回复问题请先登录注册