Datatype Misalignment
2010-06-12 16:56:03| 分类:
R&D
| 标签:
|举报
|字号大中小 订阅
Why is Alignment Important?
exceptions & crash
performance penalty
How to 'Align' everything?
In general, alignment errors can be avoided by following these rules:
* Do not enable structure packing.
The rule is don't mess with how the compiler lays out fields in a struct.
If you need to pack more data into less space, re-arrange the data so the compiler doesn't have to waste space on padding.
The easiest way to do this is to always organize your fields according to size: largest to smallest.
For C++ this applies to classes and structs.
* Do not access a small-aligned address by using a recast pointer of larger alignment.
The easiest way is to just not fool with the compiler/OS.
The compiler will automatically layout locals and parameters on properly aligned addresses.
The same goes for malloc, realloc, new, etc.
The problem arises when you allocate one thing, but then treat it like something else.
评论这张
转发至微博
转发至微博
评论