时间值没有返回预期的结果

timeval not returning expecting results

本文关键字:结果 返回 时间      更新时间:2023-10-16

我有一些代码如下:

#include <stdio.h>
#include <sys/time.h>
typedef struct{
     struct timeval timestamp;
}teststruct;
class TestClass {
     public:
       TestClass();
       void dosomething(int, int);
};
TestClass::TestClass(){
}
void
TestClass::dosomething(int num, int numb) {
}
int main(void){
     TestClass *testclass = new TestClass();
     teststruct test;
     gettimeofday(&test.timestamp, NULL);
     printf("%llu n", test.timestamp.tv_sec);
     testclass->dosomething(1,1);
     printf("%llu n", test.timestamp.tv_sec);
}

该代码的输出是:

13825459612132795564dosomething5598307500

但是我不知道为什么第一个数字是乱的。此外,为了使数字彼此不同,类调用是完全必要的。

我得到warning: format ‘%llu’ expects type ‘long long unsigned int’, but argument 2 has type ‘__time_t’。应该是个提示。将编译器的警告级别提高到合理的级别。

当您使用正确的输入类型时,它可以工作。否则你会调用UB,读取不属于你的内存;像这样的错误可能会产生有趣的结果,这些结果基于您通常不会期望产生差异的因素,如内存的内容发生变化。

如果您将%llu更改为%lu,则似乎可以工作。

http://codepad.org/YGubabLR

相关文章: