为什么 gettimeofday() 和 time_t 没有意义?
为什么如果我执行以下操作:
struct timeval time;
gettimeofday(&time, NULL);
log("time seconds %i useconds %i", sizeof(time.tv_seconds), sizeof(time.tv_usec));
Does it return in openwrt:
time seconds 4 useconds 4259840
Is time.tv_usec
using 4259840 bytes? tv_seconds
(自纪元以来)是有意义的,因为它是一个 long long int。但 tv_usec
应始终低于 100 万。
Why if I do the following:
struct timeval time;
gettimeofday(&time, NULL);
log("time seconds %i useconds %i", sizeof(time.tv_seconds), sizeof(time.tv_usec));
Does it return in openwrt:
time seconds 4 useconds 4259840
Is time.tv_usec
using 4259840 bytes? tv_seconds
(since epoch) makes sense since it is a long long int. But tv_usec
should always be below 1 million.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sizeof
运算符返回一个size_t
对象。您应该使用%zu
格式来打印它,而不是%i
。由于格式字符串和参数类型不匹配,您很可能会遇到一些参数传递错误。如果您提高编译器的警告级别,您应该会收到有关它的警告。例如,clang 告诉我:当然,这可能取决于您的 log() 函数的实现方式 - 这也可能是您的错误的根源。
The
sizeof
operator gives you back asize_t
object. You should be using the%zu
format to print it out, not%i
. It's quite possible you're having some argument passing mishap as a result of the mismatched format strings and argument types. If you crank up the warning level on your compiler, you should get a warning about it. For example, clang tells me:Of course, that may depend on how your
log()
function is implemented - which also might be the source of your bug.凝视我的水晶球......问题是你的日志函数错误地解析了它的参数。尝试使用 printf 代替。
Peering into my crystal ball...the problem is that your log function is incorrectly parsing its arguments. Try printf instead.