0

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

struct timeval time;

gettimeofday(&time, NULL);

log("time seconds %i useconds %i", sizeof(time.tv_seconds), sizeof(time.tv_usec));

它是否在openwrt中返回:

time seconds 4 useconds 4259840

是否time.tv_usec使用 4259840 字节? tv_seconds(从纪元开始)是有意义的,因为它是一个 long long int。但tv_usec应始终低于 100 万。

4

2 回答 2

3

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()功能是如何实现的——这也可能是您的错误的来源。

于 2012-02-02T01:56:29.017 回答
1

凝视我的水晶球......问题是您的日志函数错误地解析了它的参数。改用 printf 。

于 2012-02-02T01:59:22.023 回答