sizeof()注意事项

sizeof是C/C++中的一个操作符,简单的说其作用就是返回一个对象或者类型所占的内存字节数。

其返回值类型为size_t,在头文件stddef.h中定义。这是一个依赖于编译系统的值。

以下测试均在VS2010 Win32环境下,不同编译系统可能占用内存略有不同。

1.sizeof(数值):结果返回数值的数据类型大小。

printf("%d Bytes\n",sizeof(6));

 结果: 4 Bytes

printf("%d Bytes\n",sizeof(8+6.6));

 结果:8 Bytes

 2.sizeof(函数):结果返回函数返回类型的大小,且不会执行函数。

short test1(){
	printf("test1\n");
	return 0;
}

int main(){
    printf("%d Bytes\n",sizeof(test1()));
    return 0;
}

 结果:2 Bytes

 3.sizeof(数据类型):结果返回数据类型的大小。

printf("%d Bytes\n",sizeof(int));

结果:4 Bytes

4.sizeof(结构体/结构体变量):结果返回结构体的大小。

struct ab{
	int a;
	char b;
};

int main(){
    ab ab_temp;
    printf("%d Bytes , %d Bytes\n",sizeof(ab),sizeof(ab_temp));
    return 0;
}

结果:8 Bytes , 8Bytes

注意:这里结构体的大小是会自行填充至最大类型大小的整数倍,所以不为5Bytes,而为8Bytes!

5.sizeof(数组) :结果返回数组的大小。

short usArray[10];
printf("%d Bytes\n",sizeof(usArray));

结果:20 Bytes

注意:这里返回的数组的占用内存的大小,而不是数组的元素个数的多少,所以这里结果不是10。如果要求元素个数是多少,可以用如下方式:

int Count=sizeof(usArray)/sizeof(usArray[0]);

6.sizeof(类/对象):结果返回类的大小。

class class_T
{
public:
	int i;
	char a;
};

int main(){
    class_T cc;
    printf("%d Bytes , %d Bytes\n",sizeof(class_T),sizeof(cc));
    return 0;
}

 结果: 8 Bytes , 8 Bytes

注意:这里结构体的大小是会自行填充至最大类型大小的整数倍,所以不为5Bytes,而为8Bytes! 

7.sizeof(指针变量):结果返回指针的大小。

struct abc{
		int a;
		char b;
};

class class_T
{
public:
	int i;
	char a;
};

short Func_Test1(){
	printf("test1\n");
	return 0;
}

int main(){
    class_T* class_Temp;//类指针
	char* pcArray0={"1234567890"};//字符串指针
	unsigned short* pusArray1=new unsigned short[226];//数组指针1
	unsigned short* pusArray2=(unsigned short*)malloc(100);//数组指针2
	abc* abc_Temp;//结构体指针
	char** pp_StrArray=new char*[6];//二维数组的指针
	for(int i=0;i<6;i++){
		pp_StrArray[i]=(char*)malloc(100);
	}
	short (*p_Fun)()=Func_Test1;//函数指针

	printf("%d Bytes , %d Bytes , %d Bytes , %d Bytes , %d Bytes , \
%d Bytes , %d Bytes , %d Bytes\n",
		sizeof(class_Temp),sizeof(pcArray0),sizeof(pusArray1),sizeof(pusArray2),
sizeof(abc_Temp),sizeof(pp_StrArray),sizeof(pp_StrArray[2]),sizeof(p_Fun));

	if(pusArray1){
		delete [] pusArray1;
		pusArray1=NULL;
	}
	if(pusArray2){
		free(pusArray2);
		pusArray2=NULL;
	}
	for(int i=0;i<6;i++){
		if(pp_StrArray[i]){
			free(pp_StrArray[i]);
			pp_StrArray[i]=NULL;
		}
	}
	if(pp_StrArray){
		delete [] pp_StrArray;
		pp_StrArray=NULL;
	}
    
    return 0;
}

结果:  4 Bytes , 4 Bytes , 4 Bytes , 4 Bytes , 4 Bytes , 4 Bytes , 4 Bytes , 4 Bytes

注意:不管任何类型的指针,在当前编译系统下的指针长度都为 4Bytes.

如果要获取指针指向的值的大小,输出如下语句:

printf("%d Bytes , %d Bytes , %d Bytes , %d Bytes , %d Bytes , %d Bytes , \
%d Bytes , %d Bytes\n",
		sizeof(*class_Temp),sizeof(*pcArray0),sizeof(*pusArray1),sizeof(*pusArray2),
sizeof(*abc_Temp),sizeof(*pp_StrArray),sizeof(**pp_StrArray),sizeof(p_Fun()));

结果:8 Bytes , 1 Bytes , 2 Bytes , 2 Bytes , 8 Bytes , 4 Bytes , 1 Bytes , 2 Bytes

注意:可以发现,sizeof( 数组指针指向的值) 获取的是第一个元素的大小,而不是整体数值的大小。其它指针指向的值获取的是它类型的大小。

 注意:如果你想要获得一个变量的整体大小,那这个变量得是存在 栈 里的,否则它只能获取第一个元素的类型的大小。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值