时间值没有返回预期的结果
timeval not returning expecting results
我有一些代码如下:
#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
,则似乎可以工作。
相关文章:
- 如何返回一个类的两个对象相加的结果
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++Brute Force攻击函数不会返回结果
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- c++ lambda:柯里和函数:使用按值捕获与按引用捕获返回不同的结果
- C++ 获取函数在常量引用中按值返回的结果
- 如何在一个表达式中生成并返回结果?
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 三元运算符在返回语句中给出意外的结果
- 为什么函数 tellg() 没有返回好的结果?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法将 for 循环结果返回到像三元运算符这样的函数中?
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- 检查文本文件中的 0 或 1,并根据结果返回
- 如何使用boost::any_range连接多个boost范围并作为函数w/o的结果返回
- 如何从线程的结果返回Qt对象(Qtfutur)
- C++:当在函数内部创建新对象并将其作为结果返回时,我必须使用新运算符来创建对象