为什么 gettimeofday() 和 time_t 没有意义?

发布于 2025-01-01 17:32:34 字数 430 浏览 5 评论 0原文

为什么如果我执行以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2025-01-08 17:32:34

sizeof 运算符返回一个 size_t 对象。您应该使用 %zu 格式来打印它,而不是 %i。由于格式字符串和参数类型不匹配,您很可能会遇到一些参数传递错误。如果您提高编译器的警告级别,您应该会收到有关它的警告。例如,clang 告诉我:

example.c:7:25: error: conversion specifies type 'int' but the argument has type
      'unsigned long' [-Werror,-Wformat]
  printf("time seconds %i useconds %i\n", sizeof(time.tv_sec), ...
                       ~^                 ~~~~~~~~~~~~~~~~~~~
                       %lu                                     

当然,这可能取决于您的 log() 函数的实现方式 - 这也可能是您的错误的根源。

The sizeof operator gives you back a size_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:

example.c:7:25: error: conversion specifies type 'int' but the argument has type
      'unsigned long' [-Werror,-Wformat]
  printf("time seconds %i useconds %i\n", sizeof(time.tv_sec), ...
                       ~^                 ~~~~~~~~~~~~~~~~~~~
                       %lu                                     

Of course, that may depend on how your log() function is implemented - which also might be the source of your bug.

酒废 2025-01-08 17:32:34

凝视我的水晶球......问题是你的日志函数错误地解析了它的参数。尝试使用 printf 代替。

Peering into my crystal ball...the problem is that your log function is incorrectly parsing its arguments. Try printf instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文