C和指针之结构体大小和成员变量位置距离结构开始存储的位置偏移字节
2017-11-27 22:41
399 查看
1、问题
1)、结构体大小
结构体成员的内存分配满足下面三个条件2 结构体第一个成员的地址和结构体的首地址相同
3 结构体每个成员地址相对于结构体首地址的偏移量是该成员大小的整数倍,如果不是则编译器会在成员之间添加填充字节
4 结构体总的大小要是其成员中最大size的整数倍,如果不是编译器会在其末尾添加填充字节
2)、成员变量位置距离结构开始存储的位置偏移字节
我们用offsetof函数2、测试Demo
#include <stdio.h>#include <stddef.h>
#define PRODUCT_SIZE 20
struct A
{
int a;
char b;
char d;
};
struct AA
{
char a;
int b;
char c;
};
struct B
{
char a;
double b;
char c;
char d[9];
int e;
};
typedef struct
{
char product[PRODUCT_SIZE];
int qunatity;
float unit_price;
float total_amount;
char s;
} Transaction;
struct C
{
int a;
char b;
short c;
double d;
int f;
};
int main()
{
printf("float size is %d\n", sizeof(float));
printf("short size is %d\n", sizeof(short));
printf("long size is %d\n", sizeof(long));
printf("double size is %d\n", sizeof(double));
//结构体每个成员地址相对于结构体首地址的偏移量是该成员大小的整数倍,如果不是则编译器会在成员之间添加填充字节
//结构体总的大小要是其基本数据成员中最大size的整数倍,如果不是编译器会在其末尾添加填充字节
printf("A size is %d\n", sizeof(struct A));
printf("AA size is %d\n", sizeof(struct AA));
printf("B size is %d\n", sizeof(struct B));
printf("C size is %d\n", sizeof(struct C));
//offsetof表示这个指定成员开始存储的位置距离结构开始存储的位置偏移几个字节
printf("B a offsetof is %d\n", offsetof(struct B, a));
printf("B b offsetof is %d\n", offsetof(struct B, b));
printf("B c offsetof is %d\n", offsetof(struct B, c));
printf("B d offsetof is %d\n", offsetof(struct B, d));
printf("B e offsetof is %d\n", offsetof(struct B, e));
printf("Transaction size is %d\n", sizeof(Transaction));
printf("C produce offsetof is %d\n", offsetof(Transaction, product));
printf("C qunatity offsetof is %d\n", offsetof(Transaction, qunatity));
printf("C unit_price offsetof is %d\n", offsetof(Transaction, unit_price));
printf("C total_amount offsetof is %d\n", offsetof(Transaction, total_amount));
printf("C s offsetof is %d\n", offsetof(Transaction, s));
return 0;
}
3、运行结果
float size is 4short size is 2
long size is 8
double size is 8
A size is 8
AA size is 12
B size is 32
C size is 24
B a offsetof is 0
B b offsetof is 8
B c offsetof is 16
B d offsetof is 17
B e offsetof is 28
Transaction size is 36
C produce offsetof is 0
C qunatity offsetof is 20
C unit_price offsetof is 24
C total_amount offsetof is 28
C s offsetof is 32
相关文章推荐
- linux ftell函数(用于得到文件位置指针当前位置相对于文件首的偏移字节数)计算文件的字节大小
- 习题 8.19(1) 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针(地址),指向字符串开始的空间。new(n)表示分配n个字节的内存空间。
- 类的成员变量偏移指针和成员函数指针
- C++ 基本语法之变量(指针)与数据类型(占用字节大小)
- 结构体内可以包含自身的指针(链表实现方法)或 引用 但不可以包含自身变量即递归 (因为这样一来结构体大小就无法确定了)
- 函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数
- 如何计算结构体大小和结构体中成员的内存偏移
- C++中通过指针修改类内私有成员变量 & 类的大小
- 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针(地址),指向字符串开始的空间。new(n)表示分配n个字节的内存空间。
- 类的成员变量偏移指针和成员函数指针 .
- STM32 堆栈大小详解 以及变量存储位置
- 再谈sizeof——struct 结构存储空间的大小:与成员的顺序、#progma pack参数有关系
- 2.2 分别定义int,short类型的变量各一个,并依次输出它们的存储空间大小
- 用引用计数管理指针成员变量
- vim使用—移动到文件开始和结束位置和当前位置(gg,G)、快速定位到当前光标所在变量或函数的定义处(gd)、自动对齐C和C++程序(先gg再=再G)、自动补全变量名,函数名和字符串ctrl+n或ct
- [转载]JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
- 打印全局、局部、static变量的地址,确定其存储位置
- C++中RTTI动态类型识别、静态成员、变量的四种存储方式
- 将从iPos位置开始的2个字节合并成1个,在CString中显示
- 堆和栈 各种变量存储位置