gpt4 book ai didi

c - times() 系统调用。返回值溢出

转载 作者:行者123 更新时间:2023-11-30 20:30:19 26 4
gpt4 key购买 nike

如果可能的clock_t(又名signed long)范围溢出,将返回哪个值?

假设我使用内核 2.6 版本,arch 是 x86(32 位)。sizeof(signed long) = 4 字节。最大值 = 2147483647。

根据手册页 - here ,系统调用 times() 返回自过去任意点以来已经过的时钟滴答数。

“注释”部分中提到以下内容:“在 Linux 上,测量 times() 返回值的“过去的任意点”在不同的内核版本中有所不同。从 Linux 2.6 开始,该点为 (2^32/HZ) - 系统启动时间前 300 秒”

所以,我不明白十进制表示中的什么值将是起点(假设 HZ = 100)。以及溢出后的返回值是多少。

最佳答案

由于我还无法发表评论,我希望这个答案有一定用处..根据此 man

times() returns the number of clock ticks that have elapsed since an arbitrary point in the past. The return value may overflow the possible range of type clock_t. On error, (clock_t) -1 is returned, and errno is set appropriately.

所以它可以溢出,是的,当它溢出时,如果返回的是一个unsigned long,它会从+2,147,483,647−2,147,483,647 并继续向正方向变动。然而,有符号整数在溢出后未定义,这是由于编译器以不同方式处理它们以进行优化,here .

如果我的假设是正确的,它可能会多次溢出,这意味着如果无符号,则溢出后您的值可能无效,或者该值将是未定义的(随机)如果签名

关于c - times() 系统调用。返回值溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com