簡體   English   中英

錯誤的結果導致GCC / ARM中的子項很長

[英]Wrong result in sub with long long in GCC/ARM

對於ARM(Android)的C程序,我的行為非常奇怪。 我有下一個代碼:

typedef long long unsigned hwtime_t;
hwtime_t hw_timer_get_hwtime (void);

int main(int argc, char** argv) 
{
   hwtime_t m1, m2, res;  
   printf("Reading hw_timer...\n");
   m1 = hw_timer_get_hwtime();
   sleep(3);
   m2 = hw_timer_get_hwtime();
   res = m2-m1;   
   printf("Res: %llu \n",res);
   printf("Res: %llx \n",res);   
   return 0;
}

函數hw_timer_get_hwtime()在另一個文件中定義:

hwtime_t hw_timer_get_hwtime (void) {
  struct timespec clock_time;

  linux_clock_gettime (CLOCK_MONOTONIC, &clock_time);
  return timespec_to_ns (&clock_time);

}

並且timespec_to_ns是:

typedef long long unsigned hwtime_t;

#define NSEC_PER_SEC 1000000000L

inline hwtime_t timespec_to_ns(const struct timespec *ts)
{
    return ((hwtime_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
} 

問題是該操作:“ res = m2-m1”多次給出不好的結果。 例如:

m1= 9906000991679
m2= 9909026641585
and m2-m1 should be 3025649906 but I get 18446744072440234226

幾次我都得到正確的結果。 我不知道為什么會這樣。 我正在使用arm-linux-androideabi-gcc進行編譯,並在Nexus 5中執行。

我找到了解決方案,問題出在使用函數的文件中,它名為timespec_to_ns ,但我尚未聲明函數頭,因此編譯器將返回值視為整數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM