您的位置:首页 > 其它

sizeof的运用

2014-02-25 15:20 357 查看
sizeof关键字的运用 & 各个数据类型的内存分配

一、简单基本类型(32位系统下)

(1)int sizeof(int) //4

(2)char sizeof(char) //1

(3)float sizeof(float) //4

(4)bool sizeof(bool) //1

(5)long sizeof(long) //4 !!!!

二、复合数据类型(32位系统)

1. 结构体struct类型

1.1 struct类型内存空间分配遵循叠加原则、对齐原则;

内存对齐的规则:

a.第一个成员起始于0偏移处

b.每个成员按其类型大小和指定对齐参数n中的最大进行对齐

c.结构体总长度必须为所有对齐参数的整数倍

d.对于数组,可以拆开看做n个数组元素

1.2结构体中的结构体

struct str1

{

char a;

int b;

};

struct str2

{

short c;

str1 str;

double d;

};

printf("size of the long %d ",sizeof(long)); //4

printf("size of the str1 %d ",sizeof(str1)); // 8

printf("size of the str2 %d ",sizeof(str2)); // 24

结构体的中的结构体必须作为整体,不能分割,因此,在str2中的,double占用8个字节,str1又占8个字节,short c只能占用8个字节
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: