计算机系统-实验三-Q1-linux

要求:在linux-gcc下完成以下题目,熟悉整数数值在计算机内部的表示方式。

预备知识:
1.gcc常用指令及相关知识
2.C语言:offsetof()的用法
3.细说 #pragma pack(n) (attribute((aligned(8)))的作用;"__"是两个下划线)

  1. 下述两个结构所占存储空间多大?结构中各分量所在位置相对于结构起始位置的偏移量是多少?请编写程序以验证你的答案。若使用#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。

参考:
如何知道结构体中某个成员相对于结构体||类起始位置的偏移量
关于C语言中结构体所占空间大小的问题汇总

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值