天杀的内存对齐

#include<iostream> using namespace std; struct information { float f; int i; char c; }; struct info { double b; char c; }; int main() { double b; char c; cout << "sizeof( struct information) = " << sizeof ( struct information) << endl; cout << "sizeof( struct info) = " << sizeof( struct info) << endl; cout << "sizeof(b) = " << sizeof b << endl; cout << "sizeof (c) = " << sizeof c << endl; return 0; }

运行结果是:

sizeof( struct information) = 12
sizeof( struct info) = 16
sizeof(b) = 8
sizeof (c) = 1
请按任意键继续. . .

#include<iostream> using namespace std; class base { public: base(){ cout << "this is construction of base " << endl ; } ~base(){ cout << " this is destruction of base " << endl ; } private: int a ; float f ; }; class derived: public base{ public: derived(){ cout << " this is construction of derived" << endl ; } ~derived(){ cout << "this is destruction of derived " << endl ; } private: double d; char c; static int tp ; void fun(){ int a ; } }; int main(){ cout << "sizeof(class base) = " << sizeof(base) << endl ; cout << "sizeof(class derived)=" << sizeof(derived) << endl ; return 0 ; }


其结果是:

sizeof(class base) = 8
sizeof(class derived)=24
请按任意键继续. . .

关于内存对齐还有一点要注意的是:

sizeof不计算static变量,函数不占内存,但是虚函数就占内存了,而且无论多少虚函数,都只占4个字节。Just because there haveonly one vptr.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值