在运行为 MIPS 编译的程序时,我偶然发现了一个非常奇怪的问题。以下代码片段用于获取时间纪元并将其以微秒精度存储在unsigned long long
变量中。
该变量能够存储我检查过的 8 个字节sizeof(unsigned long long)
。
这段代码奇怪地打印出来:
unsigned long long microtime=0;
struct timeval time_camera = { .tv_sec=0, .tv_usec=0 };
gettimeofday(&time_camera,NULL);
microtime = time_camera.tv_sec * 1000000 + time_camera.tv_usec;
printf("Times is now %llu , time since epoch in seconds is: %lu\n", microtime, time_camera.tv_sec);
它给了我以下输出:
>> Times is now 484305845 , time since epoch in seconds is: 1357751315
但是,当我将计算分成不同的行时,它可以工作!:
unsigned long long microtime=0;
struct timeval time_camera = { .tv_sec=0, .tv_usec=0 };
gettimeofday(&time_camera,NULL);
microtime = time_camera.tv_sec;
microtime = microtime * 1000000;
microtime = microtime + time_camera.tv_usec;
printf("Times is now %llu , time since epoch in seconds is: %lu\n", microtime, time_camera.tv_sec);
输出是:
Times is now 1357751437422143 , time since epoch in seconds is: 1357751437
现在这只是一个巧合吗?例如,我是否损坏了内存或实际上超出了某个地方?也许是 MIPS 编译器?任何帮助表示赞赏!