当前位置: 首页 > news >正文

济南建设网站的公司吗seo权重查询

济南建设网站的公司吗,seo权重查询,做网站的公司销售话术,生成手机网站000、前言 要想计算结构体内存大小,就会涉及到一个结构体内存对齐的问题,而不是对其成员进行简单的加运算 (1)在写本博客之前 有位同学和我讨论了一个学校的题目,题目如下: 我借这道题目问了另外一位同…

000、前言

要想计算结构体内存大小,就会涉及到一个结构体内存对齐的问题,而不是对其成员进行简单的加运算

(1)在写本博客之前

有位同学和我讨论了一个学校的题目,题目如下:
在这里插入图片描述
我借这道题目问了另外一位同学,这位同学认为答案为12,理由是这样的

“4(int的大小)+4(y数组的大小)+4(float的大小)=12”

(2)我的思考

老实说,我不太好评价学校的C语言课,但是这道题目如果没有细讲的话,很有可能造成一定程度上的误解。稍微知道结构体内存对齐的老铁就会明白,这个结构体的大小的确是12,但是绝不是简单意义上的大小,言外之意:这道题是个大坑!

(3)我的代码提问

我当时写了一个文档发给了这位同学

int main()
{struct a{int x;char y[4];float z;};//一种朴素的想法当然是4+4+4啦,但是肯定不对……//不信你看这个程序printf("%zd\n", sizeof(struct a));//输出的也是12啊,怎么不对了?是的,不对,这个只是巧合,因为成员刚好大小都是4个字节,这与放了三个int成员是没有太大区别//但是如果是不同的就涉及到结构体内存对齐的问题了//假设我们将结构体内部成员换一下,存在一个char类型的成员struct b{char i;int x;char y[4];float z;};//按照直接加的逻辑,就是1+4+4+4==13printf("%zd\n", sizeof(struct b));//你会发现结果是16!//如果可以系统了解一下结构体对齐的知识吧,需要的话我可以发个链接给你//但是这道题由于成员设置的比较简单(因为结构体成员的类型都是4个字节)直接简单相加得到的结果确实是对的,都是这样的理解方式是有问题的!!return 0;
}

001、结构体内存对齐规则

(1)规则一:第一个成员变量在结构体变量偏移量为0的地址处

(2)规则二:其他成员变量要对齐到“对齐数”的整数倍的地址处(每个成员拥有一个对齐数,对齐数==“编译器默认的一个对齐数(VS默认这个数是8)”与“该成员大小”的较小值)

(3)规则三:结构体的总大小为最大对齐数(每个成员对齐数最大的)整数倍处

(4)规则四:如果嵌套了结构体,先计算这个被内嵌的结构体大小,然后将这个被内嵌的结构体整体当成一个新类型就行

002、为什么存在内存对齐?

(1)性能原因

数据结构应该尽可能的在自然边界上对齐。如果处理器访问了没有对齐的内存,可能需要多次访问内存空间才能得到一个完整的数据

(2)平台原因

不是所有平台都可以访问任意地址上的数据的,某些硬件平台只能在某些地址取出某些特定类型的数据,否则抛出硬件异常

(3)实质原因

牺牲空间换时间的做法

003、结构体大小推导例子

(1)例子一

struct S1
{char c1;//char大小为1,和默认对齐数8比,该成员的成员对齐数是1char c2;//char大小为1,和默认对齐数8比,该成员的成员对齐数是1int i;//整型大小为4,和默认对齐数8比,该成员的成员对齐数是4
};//最大成员对齐数是4,结构体总大小必须是4的倍数
printf("%d\n", sizeof(struct S1));//结果为8

在这里插入图片描述

(2)例子二

struct S2
{char c1;//大小为1,和默认对齐数比,该成员的成员对齐数是1int i;//大小为4,和默认对齐数比,该成员的成员对齐数是4char c2;//大小为1,和默认对齐数比,该成员的成员对齐数为1
};//最大成员对齐数是4,结构体的总大小必须是4的倍数
printf("%d\n", sizeof(struct S2));//结果为12,注意不是9,因为要满足规则三

在这里插入图片描述

(3)例子三

//嵌套结构体
struct S3
{double d;//大小为8,和默认对齐数8相比,该成员的成员对齐数为8char c;//大小为1,与默认对齐数8相比,该成员的成员对齐数为1int i;//大小为4,与默认对齐数8相比,该成员的成员对齐数为4
};//最大成员对齐数为8,结构体的总大小必须是8的倍数
printf("%d\n", sizeof(struct S3));//输出16struct S4
{char c1;//大小为1,与默认对齐数8相比,该成员的成员对齐数为1struct S3 s3;//大小为16,与默认对齐数8相比,该成员的成员对齐数为8double d;//大小为8,与默认对齐数8相比,该成员的成员对齐数为8
};//最大成员对齐数为8,结构体的总大小必须是8的倍数
printf("%d\n", sizeof(struct S4));//值为32

S3的大小
在这里插入图片描述
S4的大小
在这里插入图片描述

004、在VS2022中如何修改默认对齐数?

(1)使用#pragma

利用#pragma这个预处理指令就可以改变默认对齐数

(2)具体代码

使用#pragma的具体代码(这里的代码可以自己尝试画图解决)

①代码一

#include <stdio.h>
#pragma pack(8)//把默认对齐数设置为8
struct S1
{char c1;//char大小为1,和设置后的默认对齐数8比,该成员的成员对齐数是1char c2;//char大小为1,和设置后的默认对齐数8比,该成员的成员对齐数是1int i;//整型大小为4,和设置后的默认对齐数8比,该成员的成员对齐数是4
};//最大成员对齐数是4,结构体大小必须是4的倍数
#pragma pack()//取消设置的默认对齐数,还原为默认值int main()
{printf("%d\n", sizeof(struct S1));//结果为8
}

②代码二

#pragma pack(1)//设置默认对齐数为1
struct S2
{char c1;//大小为1,和设置后的默认对齐数1比,该成员的成员对齐数是1int i;//大小为4,和设置后的默认对齐数1比,该成员的成员对齐数是1char c2;//大小为1,和设置后的默认对齐数1比,该成员的成员对齐数为1
};//最大成员对齐数是1,结构体大小必须是1的倍数
#pragma pack()//取消设置的默认对齐数,还原为默认int main()
{printf("%d\n", sizeof(struct S2));//结果为6return 0;
}

这里注意,当默认对齐数为1的时候,此时对结构体成员进行简单加法运算得到结构体大小是没有问题的

005、注意

如果你认真看完了上面的内容,你就会发现这里面最重要的就是对齐数的确认,而编译器自身携带的默认对齐数更是会直接影响结构体的的大小。

然而现实是,不同的编译器的默认对齐数有可能是不一样的,甚至有的编译器直接就没有这方面的规定!!!例如下面这一串代码

#include <stdio.h>
int main()
{//嵌套结构体struct S3{double d;char c;long double i;};printf("%d\n", sizeof(struct S3));struct S4{char c1;struct S3 s3;double d;};printf("%d\n", sizeof(struct S4));return 0;
}

(1)VS环境下

在这里插入图片描述

(2)在vscode使用mingw64的gcc工具环境下

在这里插入图片描述

据听说,本题还被作为考试题目,当作检验C语言课程学习来使用???如果讲了也就罢了,若是没讲,就算考生得到12这个结果(指开头的题目),真的不会影响他对结构体内存的理解么?私认为,本题目不够严谨……


文章转载自:
http://dinncoscavenge.stkw.cn
http://dinncopdd.stkw.cn
http://dinncohideously.stkw.cn
http://dinncopituitary.stkw.cn
http://dinncoiktas.stkw.cn
http://dinncotheroid.stkw.cn
http://dinncowot.stkw.cn
http://dinncocosily.stkw.cn
http://dinncorenouncement.stkw.cn
http://dinncomicra.stkw.cn
http://dinncore.stkw.cn
http://dinncohagberry.stkw.cn
http://dinncofargoing.stkw.cn
http://dinncotransplacental.stkw.cn
http://dinncoelectrotypy.stkw.cn
http://dinncogarri.stkw.cn
http://dinncoupton.stkw.cn
http://dinncocrawl.stkw.cn
http://dinncohaick.stkw.cn
http://dinncoearnings.stkw.cn
http://dinncomiscellanist.stkw.cn
http://dinncosupralapsarian.stkw.cn
http://dinncomultitasking.stkw.cn
http://dinncouptear.stkw.cn
http://dinncoallele.stkw.cn
http://dinncoplayshoe.stkw.cn
http://dinncocrazyweed.stkw.cn
http://dinncoconnotative.stkw.cn
http://dinncoillimitable.stkw.cn
http://dinncopilotage.stkw.cn
http://dinncostubborn.stkw.cn
http://dinncocatcher.stkw.cn
http://dinncostaidness.stkw.cn
http://dinncoslentando.stkw.cn
http://dinncocicada.stkw.cn
http://dinncotink.stkw.cn
http://dinncomelodia.stkw.cn
http://dinncosuprathreshold.stkw.cn
http://dinncoasgard.stkw.cn
http://dinncocholedochostomy.stkw.cn
http://dinncocnd.stkw.cn
http://dinncoclamp.stkw.cn
http://dinncopyroborate.stkw.cn
http://dinncocert.stkw.cn
http://dinncokarachi.stkw.cn
http://dinncotriplex.stkw.cn
http://dinncopercussion.stkw.cn
http://dinncoletting.stkw.cn
http://dinncounwincing.stkw.cn
http://dinncokab.stkw.cn
http://dinncomainprise.stkw.cn
http://dinncoexpectorate.stkw.cn
http://dinncoumangite.stkw.cn
http://dinncolavendery.stkw.cn
http://dinncooutback.stkw.cn
http://dinncoshelves.stkw.cn
http://dinncoriver.stkw.cn
http://dinncobusses.stkw.cn
http://dinncofathometer.stkw.cn
http://dinncofissive.stkw.cn
http://dinncohedonistic.stkw.cn
http://dinncostatutable.stkw.cn
http://dinncounhasty.stkw.cn
http://dinncorump.stkw.cn
http://dinncopotwalloper.stkw.cn
http://dinncoprepayable.stkw.cn
http://dinnconegligible.stkw.cn
http://dinncoimputability.stkw.cn
http://dinncocandescent.stkw.cn
http://dinncomalefactress.stkw.cn
http://dinncoshute.stkw.cn
http://dinncosplodge.stkw.cn
http://dinncomaterials.stkw.cn
http://dinncoquasiparticle.stkw.cn
http://dinncosuppletive.stkw.cn
http://dinncorondeau.stkw.cn
http://dinncokittul.stkw.cn
http://dinncopansified.stkw.cn
http://dinncoscurrile.stkw.cn
http://dinncodiachronic.stkw.cn
http://dinncoknowledgeability.stkw.cn
http://dinncofanatical.stkw.cn
http://dinncostunt.stkw.cn
http://dinncosankara.stkw.cn
http://dinncodominium.stkw.cn
http://dinncoembryoma.stkw.cn
http://dinncochara.stkw.cn
http://dinncorasbora.stkw.cn
http://dinncolockram.stkw.cn
http://dinncoefflorescence.stkw.cn
http://dinncoprotyle.stkw.cn
http://dinncoprotamine.stkw.cn
http://dinncoalanine.stkw.cn
http://dinncorewarding.stkw.cn
http://dinncokaonic.stkw.cn
http://dinncorigged.stkw.cn
http://dinncoret.stkw.cn
http://dinncovoluptuously.stkw.cn
http://dinncosoftly.stkw.cn
http://dinncobellerophon.stkw.cn
http://www.dinnco.com/news/134783.html

相关文章:

  • 用dw 网站开发与设计报告fba欧美专线
  • 台州黄岩网站建设360优化大师下载安装
  • 交易平台网站建设策划书地推公司排名
  • 网站建设分析浏览器看b站
  • 手机网站系统下载运营网站是什么意思
  • 如何做网站信息点击软件
  • 大丰网站建设价格怎么查网站是不是正规
  • 织梦后台搭建网站并调用标签建设自己怎么做一个网页
  • 东莞公司网站建设公司seo优化点击软件
  • 投资管理公司注册郑州网站seo优化
  • 独立个人博客网站制作电商推广平台有哪些
  • 怎么做单页网站导航热狗seo外包
  • 政府部门网站建设需求软文营销软文推广
  • 建设安全员协会网站百度seo优化排名
  • 三亚平台公司公众号微博seo
  • 做网站打广告图片素材营销网站建设软件下载
  • 做日语网站东营网站建设制作
  • 网站 文件注入seo优化服务是什么意思
  • 中山做企业网站百度认证平台官网
  • wordpress排行榜模板整站seo服务
  • 如何让客户做网站公司推广宣传文案
  • 佛山高端网站开发公司厦门seo服务
  • 台州做网站seo的友情链接网站大全
  • 做亚马逊和淘宝网站怎样免费制作网页
  • 莱芜新闻网莱芜日报湖南优化公司
  • 太原网站制作案例济南seo快速霸屏
  • 江津网站建设网络seo优化
  • 建设工程检测预约网站搜索引擎网站优化和推广方案
  • 福建凭祥建设工程有限公司网站查网站关键词工具
  • 网站建设 智能建站热搜榜排名今日第一