- //为了增加Cpu寻址的效率,结构体分配内存空间遵循的两个原则(对齐原则/对齐算法)
- //1每个元素的偏移量(当前元素首地址距离当前结构体首地址的字节数)必须是自己所占字节数的整数倍
- //2整个结构体的字节数必须是其最大元素所占字节数的整数倍,如果不够,最后一个元素填充字节
- #include <stdio.h>
- int main(){
- struct Person{
- char *name;//指针,8个字节
- int age;//4字节
- };
- printf("%lu\n",sizeof(struct Person));
-
- struct Person1{
- char *name;//指针,8个字节
- int age;//4字节
- int money;//4
- };
- printf("%lu\n",sizeof(struct Person1));
-
- struct shiyan{
- int a;
- char c;
- };
- printf("%lu\n",sizeof(struct shiyan));
- struct shiyan2{
- int a;
- int b;
- int c;
- char d;
- };
- printf("%lu\n",sizeof(struct shiyan2));
-
- return 0;
- }
复制代码 |
|