一段代码输出结构不太明白

.cpp
#include
using namespace std;

class Test {
int* l;
double d;

};

int main() {
Test cs;
cout << "double =" << sizeof(double) << endl;
cout << "int* =" << sizeof(int*) << endl;
cout <<"cs ="<< sizeof cs << endl;
cin.get();
}

结果是
double =8
int* =4
cs=16
这是为什么

double =8 double占用8个字节
int* =4 基本上指针都是4个字节
cs=16 结构体占用 16个字节,说明你的编译器按照8字节对齐内存。(早期的编译器按照4字节对齐,这个不同编译器不同)

结构参数对齐问题:http://blog.csdn.net/u012243115/article/details/44563331

cs=16 结构体占用 16个字节,说明你的编译器按照8字节对齐内存。(早期的编译器按照4字节对齐,这个不同编译器不同)

sizeof()函数是判断大小的 即占内存多少字节 32位机int 一般都是4 double是8 cs结构体16

double =8 double占用8个字节
int* =4 基本上指针都是4个字节
cs=16 结构体占用 16个字节
是因为double用了8个字节;结构体里面存储是按照块的。
int* 只能按照8字节看齐。(向上看齐)

这是内存对齐的问题。