sizeof(struct)说明
来源:互联网 发布:java gbk utf8 转换 编辑:程序博客网 时间:2024/06/10 21:45
struct结构体中,对于对齐方式,首先以遇到的第一个类型对齐,之后以第二个类型对齐,之后依次,但遇到最大类型后,后面的全部以最大类型对齐。
系统如64位系统,则最大为8个字节。实例:
printf("char字节为:%d\n",sizeof(char)); //1
printf("short字节为:%d\n",sizeof(short)); //2
printf("int字节为:%d\n",sizeof(int)); //4
struct MyStruct7{
char a;
short b;
int c;
char d;
}t7; //sizeof(t7) ///此为1个字节,没有遇到更大的
struct MyStruct6{
char a;
short b;
int c;
char d;
}t6;
printf("%d\n",sizeof(t6)); //为12
首先char占据一个字节,之后遇到short 2B,则此时2B较大,故以2B补齐,此时a占2B,b占2B,遇到第三个c,4B,此时正好占满8B。
之后d为1B,但此时最大的为4B,必须补齐,故此时为12
char
str[]=
"Tencent"
; sizeof
(str)
为8 Tencent\0’,所以长度为8
har
* ch
sizeof
(ch)
其为指针,如果为win32,则为4
下面为参考善于总结的博客上,各个字节的大小;网址:http://blog.csdn.net/lyl0625/article/details/7350045
C语言中,各类型所占的字节数与机器字长及编译器有关系:
编译器类型类型占的字节数16位编译器char1B char *(指针变量)2B short int2B int2B unsigned int2B float4B double8 long4 long long8 unsigned long432位编译器char1
char*(指针变量)4B(32位的寻址空间) short int2 int4 unsigned int4 float4 double8 long
4
long long8 ungigned long464位编译器
char
1
char*(指针变量)8 short int2 int4 unsigned int4 float4 double8 long
8
long long8 unsigned long8
- sizeof(struct)说明
- sizeof(struct)
- sizeof(struct)
- sizeof(struct)
- struct sizeof
- sizeof struct
- sizeof struct
- sizeof(struct)
- sizeof(struct)
- sizeof(struct)及sizeof(class)
- sizeof理解03---sizeof(struct)
- sizeof(struct)和sizeof(union)
- sizeof(struct)与sizeof(union)
- [转]sizeof(struct)
- sizeof struct&union
- sizeof计算struct大小
- sizeof union和struct
- union, struct , sizeof. 对齐
- 【more effective c++读书笔记】【第3章】异常(2)
- Android RecyclerView 使用完全解析 体验艺术般的控件
- ORA-00001: unique constraint (...) violated并不一定是数据冲突
- Linux下创建用户命令
- 异步I/O之asyncore
- sizeof(struct)说明
- zabbix实现微信报警
- const遇到指针
- STL源码剖析 [容器](五)[stl_deque.h]
- nyoj-1216-整理图书-dp
- 用Gradle 构建你的android程序
- JavaScript学习笔记2-五种原始数据类型和Object对象类型
- Gradle, Android and the ANDROID_HOME SDK location
- 一行代码解决placeholder 不显示的问题