GCC編譯選項--"-fno-strict-aliasing"

文中的編碼方法絕對要禁止用在項目中, 最近用開源的代碼時碰到過需要這個選項的警告

這兩天編譯別的組的代碼。編譯器是GCC4.1.2,發現使用優化選項O2以上代碼執行的結果和使用O1(或不使用優化選項)不一樣,使用O1編譯出來的代碼執行結果是正確的。上網搜索了一下,發現了原因。
    代碼中有如下語句:

          float f = j;
          unsigned int* p = (unsigned int*)(&f);

    上述代碼的第二句有強制類型轉化,是出現問題的原因。原因及解決方法如下,是英文的:
     If optimization level is >= 2 in gcc-4.1, strict-aliasing is used, and this could cause probelms when a pointer is referencing to a different type of object and the object is refered there after by using this pointer. That is the case in this example. So you should force the compiler to not use strict-aliasing by a argument "-fno-strict-aliasing" if you want to use "-O2" or "-O3".
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章