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个字节
一、简单基本类型(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个字节
相关文章推荐
- qemu-img: Could not open old backing file... 分类: python学习 2014-02-25 15:20 1028人阅读 评论(0) 收藏
- 使用 Wowza IDE 开发第一个 Wowza 服务器扩展应用 -- 监控直播频道
- 如何设置socket的Connect超时
- 第一个只出现一次的字符 C++实现
- android dolby
- hbase client api 例子-2
- ocean所用的蝴蝶纹理
- 7.9.3: 容器中Bean的生命周期---协调作用域不同步的Bean
- 东南西北
- linux 进程管理,内存管理,文件系统,系统管理,网络操作概述
- asp.net读取excel文件的三种方法示例
- 指定struts2配置文件struts.xml的路径
- Apache Shiro 标签方式授权
- HttpClient 教程 (一)
- Oracle Transportable Tablespaces (TTS)
- 使用ffmpeg推流到Wowza
- EntityFramework 基础提供程序在 Open 上失败。
- Js中 关于top、clientTop、scrollTop、offsetTop的用法
- jQuery常用方法(二)-事件
- QT 4.8 静态库编译方法