sizeof举例,这回全了

本文详细探讨了C++中不同类型的变量及结构体所占用的内存大小,包括基本类型、结构体、联合体等,并通过具体示例展示了如何使用sizeof运算符来确定这些类型的确切大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "iostream"


using namespace std;
#pragma pack(4)


/*
	4字节对齐
	(1) struct{ short a; short b; short c;}A; sizeof(A) = 6
	(2) struct { long a; short b;}A; sizeof(A) = 8
	(3) struct A{ short a; long b;} struct B{ char c; A b; short d;} sizeof(B) = 16
	(4) struct A{ char c; double d; short s; int i;} sizeof(A) = 20
	(5) struct A{int a:5; int b:9; char c; int b:4; short s;} sizeof(A) = 16
	(6) union A { int a; short b; char c}; sizeof(A) = 4
	(7) union A{ int c; char c; short d; double d;} struct B{ char c; A b; double c; short d}; sizeof(B) = 24
	(8)  char a[50]; sizeof(a) =  50
	(9) char new_int[50]; sizeof(new_int) = 50
	(10) char new_int[50]; char *a = newint[50]; sizeof(*a) = 1
	(11) Class Test{int a; static double c}; sizeof(Test) = 4 ;Test * s; sizeof(s) = 4;
	(12) Class Test{}; sizeof(Test) = 1
	(13) int func(char s[5]){ return 1}; sizeof(func("12345")) = 4 ; char func(char s[5]){ return 1}; sizeof(func("12345")) = 1
	(14) sizeof(20) = 4
	(15) sizeof(20.1) = 8
	(16) char ary[100]; sizeof(ary) = 100
	(17) char *p = malloc(100); sizeof(p) = 4
	(18) char ary[] = "hello"; sizeof(ary) = 6
	(19) char *ary = "hello"; sizeof(ary) = 4
	(20) char (*fun)(int, char*) sizeof(fun) = 4
	(21) char *fun(int, char*); sizeof(fun) = 4
	(22) char(*p)[10];sizeof(p) = 4
	(23) char *p[10]; sizeof(p) = 40


	(24)char buf[2][2]; sizeof(buf) = 4 sizeof(buf[1]) = 2 sizeof(buf[0]) = 2	sizeofbuf[0][0]) = 1
	(25)int buf[2][2]; sizeof(buf) = 16 sizeof(buf[1]) = 8 sizeof(buf[0]) = 8	sizeofbuf[0][0]) = 4


*/


void fun1()
{
	struct{ short a; short b; short c;}A; cout<<"(1)struct{ short a; short b; short c;}A; sizeof(A) = "<<sizeof(A)<<endl;
}


void fun2()
{
	struct { long a; short b;}A; cout<<"(2) struct { long a; short b;}A; sizeof(A) ="<<sizeof(A)<<endl;
}


void fun3()
{
	struct A{ short a; long b;}; struct B{ char c; A b; short d;}; cout<<"(3) struct A{ short a; long b;} struct B{ char c; A b; short d;} sizeof(B) =  "<<sizeof(B)<<endl;
}


void fun4()
{
	struct A{ char c; double d; short s; int i;}; cout<<"(4) struct A{ char c; double d; short s; int i;} sizeof(A) = "<<sizeof(A)<<endl;
}


void fun5()
{
	struct A{int a:5; int b:9; char c; int d:4; short s;}; cout<<"(5) struct A{int a:5; int b:9; char c; int d:4; short s;} sizeof(A) = "<<sizeof(A)<<endl;
}


void fun6()
{
	union A { int a; short b; char c;}; cout<<"(6) union A { int a; short b; char c;}; sizeof(A) ="<<sizeof(A)<<endl;
}
void fun7()
{
	union A{ int b; char c; short d; double e;}; struct B{ char a; A b; double c; short d;}; cout<<"(7) union A{ int b; char c; short d; double e;}; struct B{ char a; A b; double c; short d;}; sizeof(B) ="<<sizeof(B)<<endl;
}
void fun8()
{
	char a[50]; char new_int[50]; cout<<"(8)char a[50]; sizeof(a) = "<<sizeof(a)<<endl<<"(9) char new_int[50]; sizeof(new_int) = "<<sizeof(new_int)<<endl;
}
void fun9(){}
void fun10()
{
	char new_int[50] = {0}; char *a = (char*)new_int[50]; cout<<"(10) char new_int[50]; char *a = new_int[50]; sizeof(*a) = "<<sizeof(*a)<<endl;
}
class TestCC{int a; static double c;};
double TestCC::c = 0;


void fun11()
{
	TestCC* s;
	cout<<"(11)Class Test{int a; static double c}; sizeof(Test) = "<<sizeof(TestCC)<<"Test * s; sizeof(s) = "<<sizeof(s)<<endl;
}


void fun12()
{
	class Test{}; cout<<"(12)Class Test{}; sizeof(Test) = "<<sizeof(Test)<<endl;
}
int func(char s[5]){ return 1;}
char funcfff(char s[5]){ return 1;}
void fun13()
{
	cout<<"(13) int func(char s[5]){ return 1}; sizeof(func(\"12345\")) = "<<sizeof(func("12345"))<<endl;
	cout<<" char func(char s[5]){ return 1}; sizeof(func(\"12345\")) = "<<sizeof(funcfff("12345"))<<endl;
}
void fun14()
{
	cout<<"(14)sizeof(20) = "<<sizeof(20)<<endl;
	cout<<"(15)sizeof(20.1) = "<<sizeof(20.1)<<endl;
	char ary[100]; cout<<"(16)char ary[100] = "<<sizeof(ary)<<endl;
}
void fun15()
{


}
void fun16()
{


}


void fun17()
{
	char *p = (char *)malloc(100); cout<<"(17) char *p = malloc(100); sizeof(p) = "<<sizeof(p)<<endl;
}
void fun18()
{
	char ary[] = "hello"; cout<<"(18)char ary[] = \"hello\"; sizeof(ary) = "<<sizeof(ary)<<endl;
}


void fun19()
{
	char *ary = "hello"; cout<<"(19) char *ary = \"hello\"; sizeof(ary) = "<<sizeof(ary)<<endl;
}




void fun20()
{
	char (*fun)(int, char*); cout<<"(20)char (*fun)(int, char*) sizeof(fun) = "<<sizeof(fun)<<endl;
}


char *funff(int a, char* p)
{
	return p;
}
void fun21()
{
	cout<<"(21) char *fun(int, char*); sizeof(fun) = "<<sizeof(&funff)<<endl;
}


void fun22()
{
	char(*p)[10]; cout<<"(22)char(*p)[10];sizeof(p) ="<<sizeof(p)<<endl;
}


void fun23()
{
	char *p[10]; cout<<"(23) char *p[10]; sizeof(p)= "<<sizeof(p)<<endl;
}


void fun24()
{
	char buf[2][2] = {1,2,3,4}; cout<<"(24)char buf[2][2]; sizeof(buf) = "<<sizeof(buf) <<" sizeof(buf[1]) = "
		<<sizeof(buf[1])<<" sizeof(buf[0]) = "<< sizeof(buf[0])<<" sizeof(buf[0][0]) = "<<sizeof(buf[0][0])<<endl ;


}
void fun25()
{
	int buf[2][2] = {1,2,3,4}; cout<<"(24)int buf[2][2]; sizeof(buf) = "<<sizeof(buf) <<" sizeof(buf[1]) = "
		<<sizeof(buf[1])<<" sizeof(buf[0]) = "<< sizeof(buf[0])<<" sizeof(buf[0][0]) = "<<sizeof(buf[0][0])<<endl ;


}


#pragma pack()






void main()
{
	fun1();
	fun2();
	fun3();
	fun4();
	fun5();
	fun6();
	fun7();
	fun8();
	fun9();
	fun10();
	fun11();
	fun12();
	fun13();
	fun14();
	fun15();
	fun16();
	fun17();
	fun18();
	fun19();
	fun20();
	fun21();
	fun22();
	fun23();
	fun24();
	fun25();
	getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值