我读了关于这个主题的指针别名规则,以及一个答案给出了下面的例子,其中提到一个潜在的问题与字节序,我想知道,如果有人可以给我什么样的字节顺序问题是在下面的代码?
struct Msg
{
unsigned int a;
unsigned int b;
};
int main()
{
// Pass data to something; if the implementer of this API were
// strict-aliasing-aware, he would have taken a char*, not a unsigned int*
Msg* msg = new Msg();
msg->a = 1;
msg->b = 2;
// stupidBuffer is an alias for msg.
// yes I know there are endianess problems here (WHY??), but my API is stupid and
// only works for one platform
unsigned int* stupidBuffer = reinterpret_cast<unsigned int*>(msg);
SendToStupidApi( stupidBuffer );
}
Run Code Online (Sandbox Code Playgroud)