在哪里可以找到关于不同平台/编译器的C ++内存对齐的文档? [英] Where can I find documentation on C++ memory alignment across different platforms/compilers?

查看:132
本文介绍了在哪里可以找到关于不同平台/编译器的C ++内存对齐的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个良好的(全面的)关于C ++内存对齐的文档,典型的方法,编译器之间的差异和常见的陷阱。

I'm looking for a good (comprehensive) doc about memory alignment in C++, typical approaches, differences between compilers, and common pitfalls. Just to check if my understanding of the topic is correct and to learn something new.

这个问题的灵感来自于我对另一个问题的回答,其中我使用了以下构造:

This question is inspired by my answer to another question where I used following construct:

char const buf[1000] = ...;
unsigned int i = *reinterpret_cast<unsigned int*>(buf + shift); // shift can be anything

它被批评为不符合内存对齐规则。你能否解释一个奖金为什么这种方法从内存对齐的角度来看是有缺陷的?一个例子,当它不工作将受到高度赞赏。我知道这是一个糟糕的方法,但我经常使用它在网络协议实现,因此它是一个比理论上更实际的问题。

It was criticized as not conforming to memory alignment rules. Can you please explain as a bonus why this approach is flawed from memory alignment point of view? An example when it doesn't work will be highly appreciated. I know it's a bad approach in general, but I often use it in network protocol implementations, so it's more a practical question than theoretical one.

还请不要提及

推荐答案

您可以在 wikipedia
在IBM站点上的更深入的内容:数据对齐:伸直向右飞

这篇关于在哪里可以找到关于不同平台/编译器的C ++内存对齐的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆