#include <iostream>struct Empty {};
struct Base { int a; };
struct Derived : Base { int b; };
struct Bit { unsigned bit: 1; };
int fun(int a)
{
return a;
}
int main()
{
Empty e;
Derived d;
Base& b = d;
Bit bit;
int a[10]={0,1,2,3,4,5,6,7,8,9};
constchar * c=(char *)malloc(6);//char *const cc=(char *)malloc(6);//char * t=(char *)malloc(6);//一维数组int * f=newint(5);
std::cout << "size of empty class: " << sizeof e << '\n'
<< "size of pointer : " << sizeof &e << '\n'// << "size of function: " << sizeof(void()) << '\n' // 错误// << "size of incomplete type: " << sizeof(int[]) << '\n' // 错误// << "size of bit field: " << sizeof bit.bit << '\n' // 错误
<< "size of array of 10 int: " << sizeof(int[10]) << '\n'
<< "size of array of 10 int (2): " << sizeof a << '\n'//数组的大小,//不是地址的大小--------注意和malloc的区分
<< "length of array of 10 int: " << ((sizeof a) / (sizeof *a)) << '\n'
<< "length of array of 10 int (2): " << ((sizeof a) / (sizeof a[0])) << '\n'
<< "size of the Derived: " << sizeof d << '\n'
<< "size of the Derived through Base: " << sizeof b << '\n'
<< "size of malloc: " << sizeof t << '\n'//指针的大小
<< "size of malloc: " << sizeof (*t) << '\n'//第一个元素的大小
<< "size of new: " << sizeof f << '\n'//指针的大小
<< "size of new: " << sizeof (*f) << '\n'//第一个元素的大小
<< "size of const: " << sizeof c << '\n'//
<< "size of const2: " << sizeof cc << '\n'////<< "size of function name: " << sizeof (fun) << '\n';//error: ISO C++ forbids applying ‘sizeof’ to an expression of function type [-fpermissive]
<<"a array use:"<<(a+2)[8]<<'\n';
}
//output(64 linux)
size of empty class: 1
size of pointer : 8
size of array of 10int: 40
size of array of 10int (2): 40
length of array of 10int: 10
length of array of 10int (2): 10
size of the Derived: 8//有两个变量 a,b
size of the Derived through Base: 4//sizeof是编译器替换,故只考虑静态类型
size of malloc: 8
size of malloc: 1
size of new: 8
size of new: 4
size of const: 8
size of const2: 8
a array use:761284352