!!!Data Padding
来源:互联网 发布:快读小说网络错误2016 编辑:程序博客网 时间:2024/05/06 22:31
Data Padding
http://en.wikipedia.org/wiki/Data_structure_alignment
Data structure members are stored sequentially in memory so that, in the structure below,the member Data1 will always precede Data2; and Data2 will always precede Data3
struct MyData{ short Data1; short Data2; short Data3;};
If the type "short" is stored in two bytes of memory then each member of the data structure depicted above would be 2-byte aligned. Data1 would be at offset 0, Data2 at offset 2 and Data3 at offset 4. The size of this structure would be 6 bytes.
The type of each member of the structure usually has a default alignment, meaning that it will, unless otherwise requested by the programmer, be aligned on a pre-determined boundary. The following typical alignments are valid for compilers from Microsoft (Visual C++), Borland/CodeGear (C++Builder), Digital Mars (DMC) and GNU (GCC) when compiling for 32-bit x86:
- A char (one byte) will be 1-byte aligned.
- A short (two bytes) will be 2-byte aligned.
- An int (four bytes) will be 4-byte aligned.
- A long (four bytes) will be 4-byte aligned.
- A float (four bytes) will be 4-byte aligned.
- A double (eight bytes) will be 8-byte aligned on Windows and 4-byte aligned on Linux (8-byte with-malign-double compile time option).
- A long long (eight bytes) will be 8-byte aligned.
- A long double (ten bytes with C++Builder and DMC, eight bytes with Visual C++, twelve bytes with GCC) will be 8-byte aligned with C++Builder, 2-byte aligned with DMC, 8-byte aligned with Visual C++ and 4-byte aligned with GCC.
- Any pointer (four bytes) will be 4-byte aligned. (e.g.: char*, int*)
The only notable difference in alignment for a 64-bit system when compared to a 32-bit system is:
- A long (eight bytes) will be 8-byte aligned.
- A double (eight bytes) will be 8-byte aligned.
- A long double (eight bytes with Visual C++, sixteen bytes with GCC) will be 8-byte aligned with Visual C++ and 16-byte aligned with GCC.
- Any pointer (eight bytes) will be 8-byte aligned.
Some data types are dependent on the implementation.
Here is a structure with members of various types, totaling 8 bytes before compilation:
struct MixedData{ char Data1; short Data2; int Data3; char Data4;};After compilation the data structure will be supplemented with padding bytes to ensure a proper alignment for each of its members:
struct MixedData /* After compilation in 32-bit x86 machine */{ char Data1; /* 1 byte */ char Padding1[1]; /* 1 byte for the following 'short' to be aligned on a 2 byte boundary assuming that the address where structure begins is an even number */ short Data2; /* 2 bytes */ int Data3; /* 4 bytes - largest structure member */ char Data4; /* 1 byte */ char Padding2[3]; /* 3 bytes to make total size of the structure 12 bytes */};
#pragma pack
http://stackoverflow.com/questions/3318410/pragma-pack-effect
- !!!Data Padding
- Structure Member Alignment, Padding and Data Packing
- Structure Member Alignment, Padding and Data Packing
- padding
- Padding
- padding
- padding
- 奇怪Scott为什么把Admin界面中Data list的表格的左右PADDING设这么大呢?
- RSA padding
- css:padding
- Padding用法
- padding oracle
- padding margin
- padding&margin
- Edge Padding
- layout_margin & padding
- margin padding
- padding笔记
- iphone 开源项目
- LeetCode: Implement strStr()
- Firefox 禁用 IPv6
- linux基础之教你如何查看linux版本
- 某个aspx页面 突然死了,连日志也没有
- !!!Data Padding
- 越狱的机器上keyWindow有时不正确
- MyEclipse配置输出控制台信息至文本文件中
- C++中的内存管理(new、delete、内存泄漏)
- C++中delete和delete[]的区别
- lua表转as3
- kruskal算法之邻接表实现
- bitmap工具类(全)
- Prim算法之邻接矩阵实现