日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

dereferencing type-punned pointer will break strict-aliasing rules

發(fā)布時(shí)間:2024/1/1 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dereferencing type-punned pointer will break strict-aliasing rules 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近遇到了一個(gè)編譯告警,嚴(yán)格別名和類型雙關(guān),整理一下相關(guān)的材料。

strict aliasing

以下問題摘自1

I have the following questions:

  • If I leave the code with this warning, will it generate potentially wrong code?
  • Is there any way to work around this problem?
  • If there isn’t, is it possible to turn off strict aliasing from inside the source file (because I don’t want to turn it off for all source files and I don’t want to make a separate Makefile rule for this source file)?
    And yes, I actually need this kind of aliasing.
  • The strict aliasing rule makes this setup illegal, two unrelated types can’t point to the same memory. char * are defined as being able to alias anything, only char* / unsigned char* has this privilege.

    aliasing assumptions

    這個(gè)告警只有在 strict aliasing optimization 開啟的情況下才會(huì)出現(xiàn),gcc 在 -O2 優(yōu)化會(huì)開啟上述優(yōu)化2,因此會(huì)出現(xiàn)該告警,忽略該告警有可能產(chǎn)生錯(cuò)誤代碼。出現(xiàn)該告警的原因是編譯器為了優(yōu)化代碼,做了一個(gè)假設(shè),即指針不會(huì)有其他類型的別名, (char *) 例外,意思是兩個(gè)類型不同的指針不會(huì)指向同一塊內(nèi)存。當(dāng)然這個(gè)特性孰好孰壞我們這里暫不討論,實(shí)際上爭(zhēng)議還是比較大的3

    Aliasing rules simply say that you can only access an object through its own type, its signed / unsigned variant type, or through a character type (signed char, unsigned char).

    如何解決

    如題主所問,如果我們確實(shí)需要做指針強(qiáng)制類型轉(zhuǎn)換,怎樣才能解決這個(gè)告警并得到健壯的代碼呢?
    C/C++ 是非常貼近硬件底層的語(yǔ)言,指針為我們編程提供了很大的便利性,同時(shí)也引入了一些隱患。我們可以很方便地重新解析一塊內(nèi)存上的數(shù)據(jù)(reinterpret) ,隱患是相伴便利性而生的,有時(shí)指針類型強(qiáng)轉(zhuǎn)并不安全,例如出現(xiàn)未初始化字節(jié)數(shù)據(jù)的問題。

    我們可以采用以下做法來消除這個(gè)告警:

    • 采用 __attribute__((__may_alias__)) 屬性去提示編譯器對(duì)某個(gè)變量關(guān)閉上面提到的假設(shè)。
    • 關(guān)閉這個(gè)假設(shè), -fno-strict-aliasing,可能影響優(yōu)化,linux kernel 有大量的指針操作,其構(gòu)建帶這個(gè)選項(xiàng)。
    • 屏蔽這個(gè)告警,-Wno-strict-aliasing,很不推薦!有掩耳盜鈴之嫌!
    • 使用 union 來解決,這也是 gcc 推薦的做法。type punning via unions feels natural。
    int retLen; someSetFunc((unsigned long*)&retLen); printf("ret len = %d\n", retLen);...union u_retLen {int retLen;unsigned long ptr; }; someSetFunc(&u_retLen.ptr); printf("ret len = %d\n", u_retLen.retLen);
  • https://stackoverflow.com/questions/4163126/dereferencing-type-punned-pointer-will-break-strict-aliasing-rules-warning/4163223 ??

  • https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Optimize-Options.html#Optimize-Options ??

  • https://lkml.org/lkml/2018/6/5/769 ??

  • 總結(jié)

    以上是生活随笔為你收集整理的dereferencing type-punned pointer will break strict-aliasing rules的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。