sizeof举例,这回全了

#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) = "<

你可能感兴趣的:(C/C++)