以下结构
typedef struct
{
short a;//2
int b;//4
char g;//1
char h;//1
double f;//8
}teststruct;在VC下发现size=24?为何不是16?struct内存分配过程是怎样的?
难道是a=4,b=4,g+h=8,f=8 4+4+8+8=24?
typedef struct
{
short a;//2
int b;//4
char g;//1
char h;//1
double f;//8
}teststruct;在VC下发现size=24?为何不是16?struct内存分配过程是怎样的?
难道是a=4,b=4,g+h=8,f=8 4+4+8+8=24?
http://man.chinaunix.net/develop/c&c++/c/c.htm里面讲的很清楚
{
short a;//2
int b;//4
char g;//1
char h;//1
double f;//8
}teststruct;
这种情况实际上是以8字节对齐的(以最宽的字段对齐)
a+b=>8,g+h=>8,f=>8
{
double a;//8
int b;//4
char g;//1
char h;//1
short f;//2
}teststruct;
在VC里sizeof(teststruct)为什么是16
typedef struct
{
short a;//2
int b;//4
char g;//1
char h;//1
double f;//8
}teststruct;在VC里sizeof(teststruct)为什么是24
如果是
typedef struct
{
int b;//4
short a;//2
char g;//1
char h;//1
double f;//8
}teststruct;
则b=4,a=2,g=1,h=1,f=8,SIZE=16如果是
typedef struct
{
double a;//8
int b;//4
char g;//1
char h;//1
short f;//2
}teststruct;
那就是a=8,b=4;g=1,h=1,f=2这样加起来是16
但如果是
typedef struct
{
double a;//8
short f;//2
int b;//4
char g;//1
char h;//1
}teststruct1;//size=16
则是a=8,f=4,b=4,g+h=8,则SIZE=24