Should I worry about the alignment during pointer casting?

前端 未结 7 1367
遇见更好的自我
遇见更好的自我 2020-11-28 05:47

In my project we have a piece of code like this:

// raw data consists of 4 ints
unsigned char data[16];
int i1, i2, i3, i4;
i1 = *((int*)data);
i2 = *((int*         


        
7条回答
  •  借酒劲吻你
    2020-11-28 06:50

    You may want to show him how things can differ depending on the compiler version:

    • GCC 4.3.4
    • GCC 4.7.2

    Apart from alignment there is a second problem: the standard allows you to cast an int* to char* but not the other way around (unless the char* was originally casted from an int*). See this post for more details.

提交回复
热议问题