我在this thread上阅读指针别名规则,一个答案给出了以下示例,其中提到了endianness的潜在问题,我想知道是否有人可以给我什么字节序问题在以下代码中?
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 );
}
答案 0 :(得分:10)
没有任何字节序问题。只要StupidApi不涉及通过网络发送或平台之间的序列化,那么根本就没有字节序问题。
答案 1 :(得分:0)
您的消息api应该允许您将本地endianess转换为网络endianess(通常是big endian)并返回。通过制定网络联结的惯例,任何机器都可以与网络上的任何一台机器通信,而不管它自己的字节顺序如何。
请查看您的api或示例。我不知道你在用什么,我担心我们无法帮助你。