计算类型大小的宏

#include <iostream>

using namespace std;

//两个指针相减,得到的是相隔几个元素
//char* 相隔几个元素,就是相隔几个字节
#define sizeof_v(x)	(char*)(&x+1) - (char*)(&x)
#define sizeof_t(t)	((size_t)((t*)0 + 1))

//对齐,内存池中,内存块大小是规则的。产生空隙碎片
#define ALIGN(v,b)	((v+b-1) & ~(b-1))

class Empty {
};


int main(void ) {
	Empty e;
	int n;
	cout << sizeof_v(e) << endl;
	cout << sizeof_v(n) << endl;
	cout << sizeof_t(Empty) << endl;
	cout << sizeof_t(int) << endl;
	
	cout << ALIGN(0, 16) <<endl;	
	cout << ALIGN(2, 16) <<endl;	
	cout << ALIGN(16, 16) <<endl;	
	cout << ALIGN(31, 16) <<endl;	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值