请问这个结构体在32位系统中占几个字节?
typedef struct _A
{
int a;
char b;
DWORD c;
_A * next;
}abc;
看它占几个字节怎么看?
问题点数:50、回复次数:8Top
1 楼99263()回复于 2002-10-12 10:59:09 得分 10
int len=0;
len = sizeof(abc);
另外,你在编译时字节对齐方式不同,这个len的结果也会不同,有1字节对齐的,也有2、4、8字节对齐的。Top
2 楼igogo(gogo)回复于 2002-10-12 11:10:55 得分 0
谢谢99263,1、2、4、8字节对齐是什么意思?Top
3 楼sjsj(虚怀若谷)回复于 2002-10-12 11:45:43 得分 10
使用sizeof可以得到单个变量的存储大小
字节对齐简单的讲:
在内存中有四个字节,为[0],[1],[2],[3]
你定义了两个char类型的变量,c1,c2
常规下是c1,c2分别对应[0],[1]
如果是2字节对齐,那么c1,c2分别对应[0],[2]Top
4 楼everandforever(Forever)回复于 2002-10-12 13:12:16 得分 10
默认的情况:
typedef struct _A
{
int a; //4字节
char b; //4字节
DWORD c; //4字节
_A * next; //4字节
}abc;Top
5 楼qing_li73(Vincent Lee)回复于 2002-10-12 13:18:26 得分 10
Just test via ur computer.....:)
Storage and Alignment of Structures
Microsoft Specific —>
Structure members are stored sequentially in the order in which they are declared: the first member has the lowest memory address and the last member the highest.
Every data object has an alignment-requirement. For structures, the requirement is the largest of its members. Every object is allocated an offset so that
offset % alignment-requirement == 0
Adjacent bit fields are packed into the same 1-, 2-, or 4-byte allocation unit if the integral types are the same size and if the next bit field fits into the current allocation unit without crossing the boundary imposed by the common alignment requirements of the bit fields.
To conserve space or to conform to existing data structures, you may want to store structures more or less compactly. The/Zp[n] compiler option and the#pragma pack control how structure data is “packed” into memory. When you use the /Zp[n] option, where n is 1, 2, 4, 8, or 16, each structure member after the first is stored on byte boundaries that are either the alignment requirement of the field or the packing size (n), whichever is smaller. Expressed as a formula, the byte boundaries are the
min( n, sizeof( item ) )
where n is the packing size expressed with the /Zp[n] option and item is the structure member. The default packing size is /Zp8.
To use the pack pragma to specify packing other than the packing specified on the command line for a particular structure, give the pack pragma, where the packing size is 1, 2, 4, 8, or 16, before the structure. To reinstate the packing given on the command line, specify the pack pragma with no arguments.
Bit fields default to size long for the Microsoft C compiler. Structure members are aligned on the size of the type or the /Zp[n] size, whichever is smaller. The default size is 4.
END Microsoft Specific
Top
6 楼igogo(gogo)回复于 2002-10-12 21:21:47 得分 0
everandforever(Forever) 为什么char型和next指针也是4个字节?Top
7 楼banalman(IT解放者)回复于 2002-10-12 21:59:54 得分 0
gzTop
8 楼everandforever(Forever)回复于 2002-10-12 22:12:52 得分 10
WIN32下指针都是4字节啊? 有问题么?
char是4字节是因为数据对齐。Top




