要求:在linux-gcc下完成以下题目,熟悉整数数值在计算机内部的表示方式。
预备知识:
1.gcc常用指令及相关知识
2.C语言:offsetof()的用法
3.细说 #pragma pack(n) (attribute((aligned(8)))的作用;"__"是两个下划线)
- 下述两个结构所占存储空间多大?结构中各分量所在位置相对于结构起始位置的偏移量是多少?请编写程序以验证你的答案。若使用#pragma pack(2)语句,则结果又如何?
struct test1
{
char x2[3];
short x3[2];
int x1;
long long x4;
};
struct test2
{
char x2[3];
short x3[2];
int x1;
long long x4;
}__attribute__((aligned(8)));
实验过程:
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<stddef.h>
//#include pragma pace(2) //只需解除注释这一行即可完成第二问
struct test1
{
char x2[3];
short x3[2];
int x1;
long long x4;
};
struct test2
{
char x2[3];
short x3[2];
int x1;
long long x4;
}__attribute__((aligned(8)));
int main()
{
printf("size of test1: %ld\n",sizeof(struct test1));
printf("size of test2: %ld\n",sizeof(struct test2));
printf("pos of test1.x2: %d\n",(int)offsetof(struct test1,x2));
printf("pos of test1.x3: %d\n",(int)offsetof(struct test1,x3));
printf("pos of test1.x1: %d\n",(int)offsetof(struct test1,x1));
printf("pos of test1.x4: %d\n",(int)offsetof(struct test1,x4));
printf("pos of test2.x2: %d\n",(int)offsetof(struct test2,x2));
printf("pos of test2.x3: %d\n",(int)offsetof(struct test2,x3));
printf("pos of test2.x1: %d\n",(int)offsetof(struct test2,x1));
printf("pos of test2.x4: %d\n",(int)offsetof(struct test2,x4));
return 0;
}
实验结果:
//#include pragma pace(2)
#include pragma pace(2)
分析:
//我选择去问老师,有懂的大佬可以在评论区回复,会及时更改。
//还是我自己写了- -
在计算机中,默认时,起始地址必须按照其自身字节大小对界。但代码中有:“attribute((aligned(8))); ”。意思是:让所有的结构成员对齐在n字节自然边界上。如果结构中有成员的长度大于n,则按照最大成员的长度来对齐。(见预备知识3)
由以上知识分析实验一,即://#pragma pack(2)
//对于struct test1:
char x2[3]; //0-2 二字节对界,在单元开头;
short x3[2]; //4-7 二字节对界,在3处加一空字节,从4开始0-7为8 % 2 == 0;
int x1; //8-11 四字节对界, 0-7为 8 % 4 == 0 刚好;
long long x4; //16-23 八字节对界,0-15 为16 % 8 == 0补12-15四字节结构体末尾在23,所以sizeof(test1)是24。
//对于struct test2: //整个结构体按8字节对齐,即结构体的末尾会停在8的倍数处。
char x2[3]; //0-2 二字节对界,在单元开头。
short x3[2]; //4-7 二字节对界,在3处加一空字节,从4开始,0-7为 8 % 2 == 0
int x1; //8-11 四字节对界, 0-7为 8 % 4 == 0 刚好
long long x4; //16-23 八字节对界,0-15 为16 % 8 == 0 补12-15四字节
//结构体末尾恰好在23,所以sizeof(test2)刚好是24。
由以上知识分析实验二,即:#pragma pack(2)
#prama pack(2)是一个伪指令,意思是使编译器强制按2字节对界。
//对于struct test1:
char x2[3]; //0-2 二字节对界,在单元开头。
short x3[2]; //4-7 二字节对界,在3处加一空字节,从4开始,0-7为 8 % 2 == 0
int x1; //8-11 二字节对界, 0-7为 8 % 2 == 0 刚好
long long x4; //12-19 二字节对界,0-11 为12 % 2 == 0,结构体末尾在19,所以sizeof(test1) == 20
//对于struct test2: //整个结构体按8字节对齐,即结构体的末尾会停在8的倍数处。
char x2[3]; //0-2 二字节对界,在单元开头。
short x3[2]; //4-7 二字节对界,在3处加一空字节,从4开始,0-7为 8 % 2 == 0
int x1; //8-11 二字节对界, 0-7为 8 % 2 == 0 刚好
long long x4; //12-19 二字节对界, 0-11 为 12 % 2 == 0
//test2结构体必须按8字节对齐,所以补齐20-23为空字节,结构体的结尾在23,即sizeof(test2) == 24。