为什么这里有一个端序问题
Why is there an endianness problem here?
我正在阅读关于指针别名规则的这个线程,一个答案给出了下面的例子,其中提到了端序的潜在问题,我想知道是否有人可以给我什么端序问题是在下面的代码?
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 );
}
不存在排序问题。只要StupidApi不涉及通过网络发送它或在平台之间进行序列化,那么根本就不存在端序问题。
消息传递api应该允许您将本地端序转换为网络端序(通常是大端序),然后再转换回来。通过对网络端序有一个约定,任何机器都可以与网络上的任何其他机器通信,而不管它自己的字节顺序如何。
查看您的api或示例。在不了解您使用的是什么产品的情况下,恐怕我们无法为您提供更多的帮助。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 我有一个关于C++提升的问题:: asio 和 std :: 异步
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 我有一个关于标准::矢量<shared_ptr>的问题
- 我有一个关于我的战斗系统HP代码的问题
- 我有一个问题,创建了C 中阻塞队列的向量
- 我有一个关于将 std::array 传递到函数中的问题
- 我有一个 c++ 代码,它在启动时崩溃,然后到达问题所在 main() 的执行
- 我的三角三元组程序有一个问题
- 在C++中有一个引用成员有什么问题?
- 为什么我有一个兼容版本问题使用libpng与Xcode
- 在这段代码中,我有一个警告:控制达到非void函数的结束[- return-type].我怎样才能解决这个问题
- 为什么这里有一个端序问题
- 轻松自如地学习OpenGL但有一个问题
- 我有一个问题,了解如何与队列工作
- std::fclose()有一个非常奇怪的问题
- devc++工作,但它有一个奇怪的问题在启动