在32位編譯環境中 sizeof的用法
sizeof(char) 1
sizeof (int) 4
sizeof(unsigned int) 4
sizeof(long int) 4
sizeof(short int) 2
sizeof(float) 4
sizeof(double) 8
2.指標變數的sizeof
char *pc="abc";
sizeof(pc) 4
sizeof(*pc) 1
int *pi
sizeof(pi) 4
sizeof(*pi) 4
char **ppc=&pc;
sizeof(ppc); 4
sizeof(*ppc) 4
sizeof(**ppc) 1
void(*pf)() sizof(pf) 4;
4陣列的sizeof
char a1="abc";
int a2[3];
sizeof(a1)//結構為4 ,還有乙個結束符
sizeof(a2)//結果為12;
結構體的sizeof
struct mystruct
double dda1; 8
char dda; 1
int type; 4
1.結構體變數中成員的偏移量必須是成員大小的整數倍
2.結構體大小必須是所有成員大小的整數倍,也即所有成員大小的公倍數。
是sizeof(char)的整數倍,sizeof(int)=4;
struct mystruct
double dda1;
char dda ;
執行結果16;
含有聯合的結構體的sizeof
struct s1
char *ptr,ch; //有指標變成4+4
union a
short a,b;
unsigned int c:2,d:1;
struct s1*next; //指標佔4
這樣執行結果是8+4=12
typedef mystruct
sizeof(mystruct)執行結果20
typdef mystruct
typedef structlength
sizeof(length)執行結果是24
typedef structlength;
sizeof(length)執行結果40;
sizeof進行結構體大小的判斷
typedef struct a t typedef struct b t typedef struct c t void main void foo4 char a4 也許當你試圖回答c4的值時已經意識到c3答錯了,是的,c3 3。這裡函式引數a3已不再是 陣列型別,而是蛻變成指標,相當於char...
sizeof進行結構體大小的判斷
typedef struct a t typedef struct b t typedef struct c t void main s1的最寬簡單成員的型別為int,s3在考慮最寬簡單型別成員時是將s1 打散 看的,所以 s3的最寬簡單型別為int,這樣,通過s3定義的變數,其儲存空間首位址需要被...
sizeof進行結構體大小的判斷
1.2.位元組對齊原則 結構體預設的位元組對齊一般滿足三個準則 1 結構體變數的首位址能夠被其最寬基本型別成員的大小所整除 2 結構體每個成員相對於結構體首位址的偏移量 offset 都是成員大小的整數倍,如有需要編譯器會在成員之間加上填充位元組 internal adding 3 結構體的總大小為...