Where can I find documentation on C++ memory alignment across different platforms/compilers?

*爱你&永不变心* 提交于 2019-12-03 06:43:13

You can find an overview on wikipedia. More in depth on the IBM site: Data alignment: Straighten up and fly right

Non-heap-allocated arrays of char have no specific requirements on their alignment. So your buffer of a thousand characters could be on an odd offset. Trying to read an int from that offset (reinterpreted as an int pointer obvious) would either result in poor performance or even a bus error on some hardware if the compiler doesn't split it up into separate read+bitmask operations.

Heap-allocated arrays of char are guaranteed to be aligned suitably to store any object type, so this is always an option.

For non-heap based storage, use boost::aligned_storage which ensures that the space is aligned properly for general use.

Imagine the case where addresses must be 16-byte aligned like for example the PS3. And then imagine that the shift == 1. This would then for sure be a non 16-byte aligned pointer which would not work on this machine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!