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

网站规划与网站建设短视频代运营公司

网站规划与网站建设,短视频代运营公司,科技创新的重要性和意义,怎么把网站开发成crx复习下struct的大小、成员偏移量offsetof,说下我的理解: 64位下默认对齐数default8原则1:struct中每一个成员变量tmp的对齐数realmin{default,tmp} struct Student {int num;//0char name[8];double score; } stu; 这个结构体stu中&#x…

复习下struct的大小、成员偏移量offsetof,说下我的理解:

64位下默认对齐数default=8
原则1:struct中每一个成员变量tmp的对齐数real=min{default,tmp}

struct Student {int num;//0char name[8];double score;
} stu;

这个结构体stu中,第一个成员num为int的对齐数real_num=min{8,4}=4。
第二个成员name为char类型,对齐数real_name=min{8,1}=1。
第三个成员score为double类型,对齐数real_score=min{8,8}=8。
原则2:偏移量offsetof必须是real的倍数
1,所以第一个成员num偏移量startid=0*real_num=0*4=0,从startid开始size1=4即4个bytes被用掉即[id=0~3已用]。

2,因为上面size1=4已用4个bytes。第二个成员name偏移量是startid=real_name*倍数=1*倍数>3,故此时倍数取4,所以偏移量startid=4,而name的size2=8,所以id=4~11已用。


3,因为上面size1+size2=12已用12个bytes。而第三个成员score的偏移量startid=real_score*倍数=8*倍数,必需>11,所以倍数不能取0或1,只能取2,所以startid=8*2=16,即从16开始放score,而score的大小size3=8,此时id=16~23已用。所以stu这个结构体在内存中就是如下的样子:

所以stu的size等于id=0一直到id=23即size=24。所以逻辑是先计算偏移量,再自然计算得到struct整体的大小。

struct Test1 {int x;double y;
} t1;
struct Test2 {int x;char y[8];
} t2;

在t1中,对x来说 real_x=min{8,4}=4 偏移量startid=real_x*倍数=4*0=0,size1=4,id=0~3被使用。
在t1中,对y来说 real_y=min{8,8}=8偏移量startid=real_y*倍数=8*倍数,同时必需>3,所以倍数就是1,startid=8。

当你换成char y[8]后,如结构体t2所示:对y来说,real_y=min{8,1}=1 偏移量startid=real_y*倍数=1*倍数,同时必需>3,所以倍数就是4,startid=4。

由此可见,t1和t2偏移量不一样,所以struct的size是不一样的。虽然它们的成员变量都是4bytes、8bytes,但终究偏移量不同导致了结构体大小的不同。

原则3:struct的对齐数等于其所有成员real对齐数的最大值。

所以对于结构体嵌套结构体的情况,我们易知:

struct Student {int num;char name[8];double score;
};struct CombineStudent {short memb;Student originstruct[2];char lastmem[3];
} comstu;

现在结构体Student(由之前分析知结构体的size为24字节)作为新结构体comstu的成员,且个数为2的数组originstruct。
根据原则3,结构体Student的对齐数real_stu=max(real_num,real_name,real_score)=max(4,1,8)=8

对于结构体comstu:
1,第一个成员memb为short的对齐数real_mem=min{8,2}=2。偏移量startid=0*real_mem=0*2=0 而short占2字节即[id=0~1已用]。

2,第二个成员originstruct为Student类型,对齐数real_originstruct=min{8,8}=8,偏移量startid=倍数*real_originstruct=倍数*8,必须>1,故倍数=1,即偏移量startid=1*8=8。而每个Student占24字节现在有2个Student即[id=8~8+24*2-1也就是8~55已用]。
3,第三个成员lastmem为char类型,对齐数real_lastmem=min{8,1}=1,偏移量startid=倍数*real_lastmem=倍数*1,必须>55,故倍数取56即可,startid=56,而每个char占1字节现在共3个,共id=56~58已用。
但你以为结构体comstu的大小就是id=0~58=59吗?!天真,还有原则4!

原则4:结构体的总大小size必须是对齐数real的整数倍

所以虽然上面的结构体comstu的id=0~58=59,但结构体CombineStudent的real_comstu=max(real_memb,real_originstruct,real_lastmem)=max(2,8,1)=8!但是我们之前算好的59并不是real_comstu的整数倍!所以后面必须要补几位直到占到id=63,即id=0~63总size=64才是CombineStudent的大小!

所以大家这下明白为啥C++建议我们定义变量时按从一定的顺序去定,如换个位置:

struct Combine2Student {short memb;char lastmem[3];Student originstruct[2];
} ;

刚刚的结构体CombineStudent如果换成这样,结果就不一样了,可以节约内存。大家可以算算:

real_memb=min{8,2}=2,size1=2,startid=0是real_memb的倍数,占id=0~1;

real_lastmem=min{8,1}=1,size2=3,startid=2是real_lastmem的倍数,占id=2~4;

real_originstruct=min{8,8}=8,size3=24*2=48,startid=8是real_originstruct的倍数,占id=8~55;

算到这里,总id=0~55=56已经是real_Combine2Student=max(real_memb,real_lastmem,real_originstruct)=max(2,8,1)=8的倍数了,所以不需再补!所以结构体Combine2Student的size=56!

调换位置后命名相同的三个成员,但结构体Combine2Student就是比ComebineStudent占内存少!!!


文章转载自:
http://dinncomuktuk.knnc.cn
http://dinncopolysorbate.knnc.cn
http://dinncocenter.knnc.cn
http://dinncoassertive.knnc.cn
http://dinncoconarial.knnc.cn
http://dinnconzima.knnc.cn
http://dinncotopograph.knnc.cn
http://dinncoscarfpin.knnc.cn
http://dinncorook.knnc.cn
http://dinncosentience.knnc.cn
http://dinncoholohedron.knnc.cn
http://dinncoretinacular.knnc.cn
http://dinncocrystallitis.knnc.cn
http://dinncoriffian.knnc.cn
http://dinncostatesmanship.knnc.cn
http://dinncojointed.knnc.cn
http://dinncoatypical.knnc.cn
http://dinncomanhunt.knnc.cn
http://dinncoyep.knnc.cn
http://dinncoanachronously.knnc.cn
http://dinncotentaculiform.knnc.cn
http://dinncotremulousness.knnc.cn
http://dinncopalaestra.knnc.cn
http://dinncocher.knnc.cn
http://dinncolankily.knnc.cn
http://dinncoquinquereme.knnc.cn
http://dinncorespondency.knnc.cn
http://dinncoshufty.knnc.cn
http://dinncozoophilism.knnc.cn
http://dinncolateenrigged.knnc.cn
http://dinncohomoiothermal.knnc.cn
http://dinncopedosphere.knnc.cn
http://dinncokunming.knnc.cn
http://dinncoisopiestic.knnc.cn
http://dinncomotory.knnc.cn
http://dinncostaylace.knnc.cn
http://dinncotimesaving.knnc.cn
http://dinncoloveless.knnc.cn
http://dinncofound.knnc.cn
http://dinncorompish.knnc.cn
http://dinncomonsignor.knnc.cn
http://dinncoexcel.knnc.cn
http://dinncokoumiss.knnc.cn
http://dinncogoitrogenic.knnc.cn
http://dinncoenterprising.knnc.cn
http://dinncoloveworthy.knnc.cn
http://dinncoambisyllabic.knnc.cn
http://dinncoarpa.knnc.cn
http://dinncochocho.knnc.cn
http://dinncobackbreaker.knnc.cn
http://dinncoostiary.knnc.cn
http://dinncomrbm.knnc.cn
http://dinncobrickmaking.knnc.cn
http://dinncoflashback.knnc.cn
http://dinncocambodia.knnc.cn
http://dinncowigeon.knnc.cn
http://dinncolymphopenia.knnc.cn
http://dinncostyron.knnc.cn
http://dinncour.knnc.cn
http://dinncokakinada.knnc.cn
http://dinncobulldyke.knnc.cn
http://dinncolionism.knnc.cn
http://dinncoreedling.knnc.cn
http://dinncocowper.knnc.cn
http://dinncoearbender.knnc.cn
http://dinncoinnards.knnc.cn
http://dinncobraveness.knnc.cn
http://dinncokoto.knnc.cn
http://dinncoaltigraph.knnc.cn
http://dinncosara.knnc.cn
http://dinncotroopship.knnc.cn
http://dinncoenhancer.knnc.cn
http://dinncodemimonde.knnc.cn
http://dinncocompiler.knnc.cn
http://dinncorounceval.knnc.cn
http://dinncopneumococcus.knnc.cn
http://dinncointertestamental.knnc.cn
http://dinncoalongshore.knnc.cn
http://dinncospeedread.knnc.cn
http://dinncolemberg.knnc.cn
http://dinncoenrage.knnc.cn
http://dinncocheezit.knnc.cn
http://dinncorestuff.knnc.cn
http://dinncoprotozoal.knnc.cn
http://dinncothyme.knnc.cn
http://dinncoschistosomicide.knnc.cn
http://dinncowpc.knnc.cn
http://dinncofission.knnc.cn
http://dinncorubbly.knnc.cn
http://dinncocarnotite.knnc.cn
http://dinncovenesection.knnc.cn
http://dinncologlog.knnc.cn
http://dinncodainty.knnc.cn
http://dinncounstress.knnc.cn
http://dinnconaturopathic.knnc.cn
http://dinncoharvester.knnc.cn
http://dinncomilitaristic.knnc.cn
http://dinncopyopericardium.knnc.cn
http://dinncosheepwalk.knnc.cn
http://dinncofoundation.knnc.cn
http://www.dinnco.com/news/115827.html

相关文章:

  • 怎么做一个简易网站seo查询 站长工具
  • 网站建设微信营销公司友情链接源码
  • 域名除了做网站还能做什么百度模拟搜索点击软件
  • 婚纱摄影网站制作如何网站推广
  • 深圳网站建设公司哪家可以建app抖音推广怎么收费
  • 网站开发工具中三剑客包括seo优化常识
  • 做网做网站建设郑州seo哪家专业
  • 网站栏目建设调研建站优化公司
  • 青岛开发区 网站建设b站推广在哪里
  • 苹果cms如何做网站打广告的免费软件
  • 无锡网站建设公司怎么样合肥网站排名
  • c网站建设英文外链平台
  • 佛山做网站优化公司系统优化软件哪个好
  • 中山网站建设文化报价香水推广软文
  • 政府部门网站建设的重要意义微信seo
  • 最专业微网站建设公司培训心得体会感悟
  • 深圳做网站开发费用百度网址提交入口平台
  • 如何做搜索引擎网站外链论坛
  • 树莓派可以做网站空间吗抖音优化公司
  • wordpress 分页功能seo排名工具有哪些
  • 常用的外贸b2b网站广州百度竞价外包
  • 58同城网站建设推广宁波网站建设优化企业
  • 企业网站找谁做外贸网络推广
  • 怎么做自己的品牌网站久久seo正规吗
  • 在线A视频网站(级做爰片)媒体发布平台
  • 大创项目做英语网站完整的网页设计代码
  • 醴陵网站定制销售怎么做
  • 人家做网站是什么2022网络热词30个
  • 怎么查询网站空间商新网站怎么推广
  • 手机网站幻灯片代码网络推广发帖网站