32位和64位系統下 int、char、long、double所佔的記憶體
阿新 • • 發佈:2021-12-31
32位和64位系統下 int、char、long、double所佔的記憶體
8位元組為上限
C型別 | 30位機器(位元組) | 64位機器(位元組) |
char | 1 | 1 |
short | 2 | 2 |
int | 4 | 4 |
long int | 4 | 8 |
long long | 8 | 8 |
char * | 4 | 8 |
float | 4 | 4 |
double | 8 | 8 |
#include<stdio.h> struct A { int a; char b; double c; char d; };struct B { char a; double b; char c; }; int main() { printf("int =%lu,char=%lu,double=%lu\n",sizeof(int),sizeof(char),sizeof(double)); printf("structA=%lu structB=%lu\n",sizeof(struct A),sizeof(struct B)); return 0; } //64位 最多可以8位位元組對齊 (先4位,遇8位變8位對齊) //32位 最多可以4位位元組對齊
//64位編譯輸出: int =4,char=1,double=8 structA=24 structB=24 // structA: 4+(1+3)+8+(1+7) = 24 // structB: (1+7)+8+(1+7) = 24 //未遇到8位資料型別時時4位元組對齊,遇到8位元組資料型別變為8位元組對齊 //32位編譯輸出: int =4,char=1,double=8 structA=20 structB=16 // struct A : 4+(1+3)+8+(1+3) = 20 // struct B : (1+3)+8+(1+3) = 16
//不管怎麼樣都只能是4位元組對齊