visual studio 字节对齐 pack应用

32位的电脑内存一般都是4 bytes对齐。

struct _ONE_BYTE_STRUCT{

char m_byte;

};

struct _ONE_BYTE_STRUCT 结构体对象占用内存 4byte;


visual studio IDE 编程使用 关键字。修改内存对齐字节数

#pragma pack(1)

//定义的结构体 1字节对齐

......//去掉内存 4 字节对齐,对结构体内存控制起到关键作用

#pragma pack(4)

//定义的结构体 4字节对齐


#pragma pack()详见MSDN说明:

#pragma pack( [ n] )

Specifies packing alignment for structure and union members. Whereas the packing alignment of structures and unions is set for an entire translation unit by the /Zp option, the packing alignment is set at the data-declaration level by the pack pragma. The pragma takes effect at the first structure or union declaration after the pragma is seen; the pragma has no effect on definitions.

When you use #pragma pack(n), where n is 1, 2, 4, 8, or 16, each structure member after the first is stored on the smaller member type or n-byte boundaries. If you use #pragma pack without an argument, structure members are packed to the value specified by /Zp. The default /Zp packing size is /Zp8.

猜你喜欢

转载自blog.csdn.net/mrlzl9/article/details/53095884