为什么sizeof(List)为20,多了两个字节?
下面的这个程序的目的是建立一个单链表.开发环境WINDOWSXP+VC6.0
我的问题是这样的:我在调试的时候获取以下的信息:
sizeof(List) 20
sizeof(Head->Number) 4
sizeof(Head->Name) 10
sizeof(Head->Next) 4
Head 0x004300b0
&Head->Number 0x004300b0
&Head->Name 0x004300b4
&Head->Next 0x004300c0
4 + 4 +10 = 18, 为什么sizeof(List)为20,多了两个字节
0x004300c0 - 0x004300b4 = c,而我只定义了10个字符,这里是不是因为在80x86中
双字的低端字节最好在能够被四整除的地址.所以编译器自动得将Name的长度加2.
如果是的话,那么这两个字节的内容是什么?在VC6.0中可不可以改变这个设置?
#include <stdlib.h>
#include <stdio.h>
#define Max 10
struct List
{
int Number;
char Name[ Max ];
struct List *Next;
};
typedef struct List Node;
typedef Node *Link;
//Function declare
Link Create_List( Link Head );
int main()
{
Link Head = NULL;
Head = Create_List( Head );
return 0;
}
Link Create_List( Link Head )
{
int DataNum = 0;
char DataName[ Max ];
Link New;
Link Pointer;
int i = 0;
Head = ( Link )malloc( sizeof( Node ) );
// allocate memory failure
if( Head == NULL )
{
printf( "Memory allocate Failure!!\n" );
}
else
{
DataNum = 1;
printf( "Please input the data name : " );
scanf( "%s", DataName );
Head->Number = DataNum;
for( i = 0; i <= Max; i++ )
{
Head->Name[ i ] = DataName[ i ];
}
Head->Next = NULL;
Pointer = Head;
while( 1 )
{
DataNum++;
New = ( Link )malloc ( sizeof( Node) );
if( New == NULL )
{
printf( "Memory allocate Failure!!\n" );
break;
}
printf( "Please input the data name : " );
scanf( "%s", DataName );
if( DataName[ 0 ] == '0' ) //input the 0 to end input.
{
break;
}
New->Number = DataNum;
for( i = 0; i <= Max; i++ )
{
New->Name[ i ] = DataName[ i ];
}
New->Next = NULL;
Pointer->Next = New;
Pointer = New;
}
}
return Head;
}