When is char* safe for strict pointer aliasing?

后端 未结 2 1789
闹比i
闹比i 2020-12-05 18:13

I\'ve been trying to understand the strict aliasing rules as they apply to the char pointer.

Here this is stated:

It is always presumed that a

2条回答
  •  生来不讨喜
    2020-12-05 18:36

    Correct, the second example is in violation of the strict aliasing rules, so if you compile with the -fstrict-aliasing flag, there's a chance you may get incorrect object code. The fully correct solution would be to use a union here:

    union
    {
      SocketMsgToRecv msg;
      char msgBuff[100];
    };
    
    recv(socket, msgBuff, 100);
    
    printf("Got Msg: a: %i, b: %i", msg.a, msg.b);
    

提交回复
热议问题