strict-aliasing

    8熱度

    2回答

    在a specific problem後面,自我回答並對其發表評論,我想了解它是否是一個正確的解決方案,解決方法/破解或者純粹是錯誤的。 具體而言,我重寫代碼: T x = ...; if (*reinterpret_cast <int*> (&x) == 0) ... 如: T x = ...; if (*reinterpret_cast <volatile int*> (&x

    6熱度

    3回答

    我有一個函數,需要一個無符號長整數*,並且需要將它傳遞給一個外部函數庫,該函數需要一個無符號整數*,並且在這個平臺上,unsigned int/long的大小相同。 void UpdateVar(unsigned long* var) { // this function will change the value at the address of var ExternalL

    4熱度

    1回答

    我有以下代碼: struct A { short b; }; struct B { double a; }; void foo (struct B* src) { struct B* b = src; struct A* a = (struct A*)src; b->a = sin(rand()); if(a->b

    18熱度

    3回答

    據我所知,GCC支持C++中的所有C99功能。但是C99嚴格別名是如何在C++代碼中處理的? 我知道用C鑄造不相關的類型不是嚴格的別名安全的,可能會產生不正確的代碼,但是C++呢?由於嚴格的別名不是C++標準的一部分(這是否正確?),GCC必須自己指定語義。 我圖const_cast和static_cast在相關類型之間轉換,因此它們是安全的,而reinterpret_cast可以打破嚴格的別名規

    23熱度

    2回答

    我一直在試圖理解嚴格的別名規則,因爲它們適用於字符指針。 Here這是說: 它總是假定一個char *可以指任何對象的別名。 好了,所以在插座代碼的情況下,我可以這樣做: struct SocketMsg { int a; int b; }; int main(int argc, char** argv) { // Some code... Soc

    11熱度

    1回答

    在回答另外一個問題,我想下面的例子: void *p; unsigned x = 17; assert(sizeof(void*) >= sizeof(unsigned)); *(unsigned*)&p = 17; // (1) memcpy(&p, &x, sizeof(x)); // (2) 1個線斷裂重疊規則。然而,第2行是可以的。別名規則。問題是:爲什麼?編譯器是否有特殊