sizeof和new的开销空间

选自:《编程珠玑第二版》附录3


/* Copyright (C) 1999 Lucent Technologies */
/* From 'Programming Pearls' by Jon Bentley */

/* spacemod.cpp -- simple model for C++ space */

#include <iostream>
using namespace std;

#define MEASURE(T, text) {					\
	cout << text << "\t";					\
	cout << sizeof(T) << "\t";				\
	int lastp = 0;							\
	for (int i = 0; i < 11; i++) {			\
		T *p = new T;						\
		int thisp = (int) p;				\
		if (lastp != 0)						\
			cout << " " << thisp - lastp;	\
		lastp = thisp;						\
	}										\
	cout << "\n";							\
}

// Must use macros; templates give funny answers

template <class T>
void measure(char *text)
{	cout << " measure: " << text << "\t";
	cout << sizeof(T) << "\n";
}

struct structc { char c; };
struct structic { int i; char c; };
struct structip { int i; structip *p; };
struct structdc { double d; char c; };
struct structcd { char c; double d; };
struct structcdc { char c1; double d; char c2; };
struct structiii { int i1; int i2; int i3; };
struct structiic { int i1; int i2; char c; };
struct structc12 { char c[12]; };
struct structc13 { char c[13]; };
struct structc28 { char c[28]; };
struct structc29 { char c[29]; };
struct structc44 { char c[44]; };
struct structc45 { char c[45]; };
struct structc60 { char c[60]; };
struct structc61 { char c[61]; };

int main()
{	cout << "Raw sizeof";
	cout << "\nsizeof(char)="     << sizeof(char);	
	cout << "  sizeof(short)="    << sizeof(short);
	cout << "  sizeof(int)="      << sizeof(int);
	cout << "\nsizeof(float)="    << sizeof(float);
	cout << "  sizeof(struct *)=" << sizeof(structc *);
	cout << "  sizeof(long)="     << sizeof(long);
	cout << "\nsizeof(double)="   << sizeof(double);

	cout << "\n\nMEASURE macro\n";
	MEASURE(int, "int");
	MEASURE(structc, "structc");
	MEASURE(structic, "structic");
	MEASURE(structip, "structip");
	MEASURE(structdc, "structdc");
	MEASURE(structcd, "structcd");
	MEASURE(structcdc, "structcdc");
	MEASURE(structiii, "structiii");
	MEASURE(structiic, "structiic");
	MEASURE(structc12, "structc12");
	MEASURE(structc13, "structc13");
	MEASURE(structc28, "structc28");
	MEASURE(structc29, "structc29");
	MEASURE(structc44, "structc44");
	MEASURE(structc45, "structc45");
	MEASURE(structc60, "structc60");
	MEASURE(structc61, "structc61");

	
	cout << "\nmeasure template (strange results)\n";
	// Uncomment below lines to see answers change
	measure<int>("int");
//	measure<structc>("structc");
//	measure<structic>("structic");
	return 0;
}

作者给某结构类型指针连续分配内存,用地址相减得出该结构类型的new的开销空间。


注意:

1、sizeof(T*),T*是指针,则大小都是4个字节(32位计算机);//  WIN32 XP

2、sizeof(T),T是结构类型,则大小都是该结构类型的空间大小(静态);//不知道怎么叙述,呵呵。

3、给某结构类型指针连续分配内存,用地址相减得出该结构类型的new的开销空间,该大小一般大于该结构类型的空间大小(静态),且有一定对齐规律性;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值