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

优秀平面设计网站网络推广是诈骗吗

优秀平面设计网站,网络推广是诈骗吗,中国室内设计师官网,手机网站源程序1.结构体内存对齐 我们已经基本掌握了结构体的使用了。那我们现在必须得知道结构体在内存中是如何存储的?内存是如何分配的?所以我们得知道如何计算结构体的大小?这就引出了我们今天所要探讨的内容:结构体内存对齐。 1.1 对齐规…

1.结构体内存对齐

我们已经基本掌握了结构体的使用了。那我们现在必须得知道结构体在内存中是如何存储的?内存是如何分配的?所以我们得知道如何计算结构体的大小?这就引出了我们今天所要探讨的内容:结构体内存对齐。

1.1 对齐规则

首先得掌握结构体的对齐规则:
1. 结构体的第⼀个成员对⻬到和结构体变量起始位置偏移量为0的地址处。
2. 其他成员变量要对⻬到某个数字(对⻬数)的整数倍的地址处。
对齐数 = 编译器默认的⼀个对⻬数 与 该成员变量大小的 较⼩值
- VS 中默认对齐数的值为 8
- Linux中 gcc 没有默认对⻬数,对⻬数就是成员⾃⾝的大小
3. 结构体总大小为最⼤对⻬数(结构体中每个成员变量都有⼀个对⻬数,所有对⻬数中最⼤的)的
整数倍。
4. 如果嵌套了结构体的情况,嵌套的结构体成员对⻬到⾃⼰的成员中最⼤对⻬数的整数倍处,结构
体的整体⼤⼩就是所有最⼤对⻬数(含嵌套结构体中成员的对⻬数)的整数倍。
范例1:
//范例1
struct S1
{char c1;//1 8 1int i;  //4 8 4char c2;//1 8 1
};int main()
{struct S1 s1 = { 0 };printf("%zd\n", sizeof(s1));return 0;
}

我们画图分析一下:

15aa98d199d8432ab9f0d6c61cdd4f6f.png

我们运行一下结果看看,是不是12个字节:

ad308321e09142eebe4307b078b41f7c.png

确实是12个字节,这就说明,结构体在内存存储中,存在内存对齐的原则。

范例2:

//范例2
struct S2
{char c1;char c2;int i;
};int main()
{struct S2 s2 = { 0 };printf("%zd\n", sizeof(s2));return 0;
}

同样的道理:

f4f48a75f2c14e3ba8d8a82a023c9309.png

运行结果:

3d07f9a5b61d4d03a514ca6385a888f1.png

范例3:

//范例3
struct S3
{double d;//8 8 8char c;  //1 8 1int i;   //4 8 4
};int main()
{struct S3 s3 = { 0 };printf("%zd\n", sizeof(s3));return 0;
}

08e42c74535f427aa4faf99a13703b04.png

运行结果:

7d29cbd2cc934f93bfdefabb731ff858.png

范例4:

//范例4
struct S3
{double d;//8 8 8char c;  //1 8 1int i;   //4 8 4
};struct S4
{char c1;struct S3 s3;double d;
};int main()
{struct S4 s4 = { 0 };printf("%zd\n", sizeof(s4));return 0;
}

65519e877cb049b681f6aee2312cfd28.png

运行结果:

feceb090dd354b35bc2b3479e5262748.png

1.2 为什么存在内存对齐?

⼤部分的参考资料都是这样说的:
1. 平台原因 (移植原因):
不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常。
2.性能原因:
数据结构(尤其是栈)应该尽可能地在⾃然边界上对⻬。原因在于,为了访问未对⻬的内存,处理器需要作两次内存访问;⽽对⻬的内存访问仅需要⼀次访问。假设⼀个处理器总是从内存中取8个字节,则地 址必须是8的倍数。如果我们能保证将所有的double类型的数据的地址都对⻬成8的倍数,那么就可以⽤⼀个内存操作来读或者写值了。否则,我们可能需要执⾏两次内存访问,因为对象可能被分放在两个8字节内存块中。
总体来说:结构体的内存对⻬是拿空间来换取时间的做法。
那在设计结构体的时候,我们既要满⾜对⻬,⼜要节省空间,如何做到:
让占⽤空间⼩的成员尽量集中在⼀起
 //例如:struct S1{char c1;//1 8 1int i;  //4 8 4char c2;//1 8 1};
//sizeof(struct S1) -> 12个字节struct S2{char c1;//1 8 1char c2;//1 8 1int i;  //4 8 4};
//sizeof(struct S2) -> 8个字节

1.3 修改默认对齐数

#pragma 这个预处理指令,可以改变编译器的默认对齐数。
#include <stdio.h>#pragma pack(1)//设置默认对⻬数为1
struct S
{char c1;int i;char c2;
};
#pragma pack()//取消设置的对⻬数,还原为默认
int main()
{//输出的结果是什么?printf("%d\n", sizeof(struct S));return 0;
}
结构体在对齐方式不合适的时候,我们可以自己更改默认对齐数。

运行结果:

ab6eb5410408467199d9cc00b576a0dc.png

2.结构体传参

struct S
{int data[1000];int num;
};
struct S s = {{1,2,3,4}, 1000};
//结构体传参
void print1(struct S s)
{printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct S* ps)
{printf("%d\n", ps->num);
}
int main()
{print1(s); //传结构体print2(&s); //传地址return 0;
}
上⾯的 print1 print2 函数哪个好些?
答案是:首选print2函数。
原因:
函数传参的时候,参数是需要压栈,会有时间和空间上的系统开销。
如果传递⼀个结构体对象的时候,结构体过⼤,参数压栈的的系统开销⽐较⼤,所以会导致性能的下降。
结论:
结构体传参的时候,要传结构体的地址。

3.结构体实现位段

结构体讲完就得讲讲结构体实现位段的能力。

3.1 什么是位段

位段的声明和结构是类似的,有两个不同:
1. 位段的成员必须是 int、unsigned int 或signed int ,在C99中位段成员的类型也可以
选择其他类型。
2. 位段的成员名后边有⼀个冒号和⼀个数字。
比如:
struct A
{int _a:2;int _b:5;int _c:10;int _d:30;
};
A就是⼀个位段类型。
那位段A所占内存的大小是多少?
printf("%d\n", sizeof(struct A));

3.2 位段的内存分配

1. 位段的成员可以是 intunsigned int signed int 或者是 char 等类型
2. 位段的空间上是按照需要以4个字节( int )或者1个字节( char )的⽅式来开辟的。
3. 位段涉及很多不确定因素,位段是不跨平台的,注重可移植的程序应该避免使⽤位段。
//⼀个例⼦
#include <stdio.h>
struct S
{char a : 3;char b : 4;char c : 5;char d : 4;
};
int main()
{struct S s = { 0 };s.a = 10;s.b = 12;s.c = 3;s.d = 4;//空间是如何开辟的?return 0;
}

f58e0bf2187f4ef3a513e6fefa651cee.png

3.3 位段的跨平台问题

1. int 位段被当成有符号数还是⽆符号数是不确定的。
2. 位段中最⼤位的数目不能确定。(16位机器最大16,32位机器最大32,写成27,在16位机器会
出问题。
3. 位段中的成员在内存中从左向右分配,还是从右向左分配,标准尚未定义。
4. 当⼀个结构包含两个位段,第⼆个位段成员⽐较大,⽆法容纳于第⼀个位段剩余的位时,是舍弃
剩余的位还是利⽤,这是不确定的。
总结:
跟结构相⽐,位段可以达到同样的效果,并且可以很好的节省空间,但是有跨平台的问题存在。

3.4 位段使用的注意事项

位段的⼏个成员共有同⼀个字节,这样有些成员的起始位置并不是某个字节的起始位置,那么这些位置处是没有地址的。内存中每个字节分配⼀个地址,⼀个字节内部的bit位是没有地址的。
所以不能对位段的成员使⽤&操作符,这样就不能使⽤scanf直接给位段的成员输⼊值,只能是先输⼊放在⼀个变量中,然后赋值给位段的成员。
struct A
{int _a : 2;int _b : 5;int _c : 10;int _d : 30;
};
int main()
{struct A sa = {0};scanf("%d", &sa._b);//这是错误的//正确的⽰范int b = 0;scanf("%d", &b);sa._b = b;return 0;
}


文章转载自:
http://dinncomelton.ssfq.cn
http://dinncowapenshaw.ssfq.cn
http://dinncoshilka.ssfq.cn
http://dinncoapprovable.ssfq.cn
http://dinncoconserve.ssfq.cn
http://dinncodioptrics.ssfq.cn
http://dinncotayal.ssfq.cn
http://dinncobreslau.ssfq.cn
http://dinncorhatany.ssfq.cn
http://dinncohale.ssfq.cn
http://dinncoarcturus.ssfq.cn
http://dinncoembryogenic.ssfq.cn
http://dinncocorniculate.ssfq.cn
http://dinncosirupy.ssfq.cn
http://dinncosquadron.ssfq.cn
http://dinncosubmicroscopic.ssfq.cn
http://dinncoapparatus.ssfq.cn
http://dinncoverapamil.ssfq.cn
http://dinncoyorker.ssfq.cn
http://dinncopappi.ssfq.cn
http://dinncosetover.ssfq.cn
http://dinncooniony.ssfq.cn
http://dinncotunellite.ssfq.cn
http://dinncoderisory.ssfq.cn
http://dinncocanadianize.ssfq.cn
http://dinncoupspring.ssfq.cn
http://dinncometainfective.ssfq.cn
http://dinncoconchiferous.ssfq.cn
http://dinnconondescript.ssfq.cn
http://dinnconecessitating.ssfq.cn
http://dinncorepagination.ssfq.cn
http://dinncothreshold.ssfq.cn
http://dinncostrewn.ssfq.cn
http://dinncosoutherly.ssfq.cn
http://dinncopud.ssfq.cn
http://dinncouppiled.ssfq.cn
http://dinncoacopic.ssfq.cn
http://dinncofrangibility.ssfq.cn
http://dinncobellwort.ssfq.cn
http://dinncoprocurement.ssfq.cn
http://dinncohyperboloid.ssfq.cn
http://dinncotalkfest.ssfq.cn
http://dinncosubvitreous.ssfq.cn
http://dinncoabolishment.ssfq.cn
http://dinncoversed.ssfq.cn
http://dinncopedocal.ssfq.cn
http://dinncopreinvasion.ssfq.cn
http://dinncozygophyllum.ssfq.cn
http://dinncoscaled.ssfq.cn
http://dinncoinh.ssfq.cn
http://dinncoethnobiology.ssfq.cn
http://dinncoloun.ssfq.cn
http://dinncobattement.ssfq.cn
http://dinncoflank.ssfq.cn
http://dinncocretinous.ssfq.cn
http://dinncorichwin.ssfq.cn
http://dinncoperiodontal.ssfq.cn
http://dinncoquintet.ssfq.cn
http://dinncocombustible.ssfq.cn
http://dinncolynx.ssfq.cn
http://dinncopratie.ssfq.cn
http://dinncosaucy.ssfq.cn
http://dinncocurtsy.ssfq.cn
http://dinncosaute.ssfq.cn
http://dinncopatsy.ssfq.cn
http://dinncononviolent.ssfq.cn
http://dinncomandrax.ssfq.cn
http://dinncorubbingstone.ssfq.cn
http://dinncoascot.ssfq.cn
http://dinncorand.ssfq.cn
http://dinncoconcessioner.ssfq.cn
http://dinncoequinoctial.ssfq.cn
http://dinncogenethlialogy.ssfq.cn
http://dinncowonderland.ssfq.cn
http://dinncosaucerful.ssfq.cn
http://dinncomorisco.ssfq.cn
http://dinncopucklike.ssfq.cn
http://dinncomantoux.ssfq.cn
http://dinncorabia.ssfq.cn
http://dinncoarpeggione.ssfq.cn
http://dinncoproofread.ssfq.cn
http://dinncodogleg.ssfq.cn
http://dinncocosmetize.ssfq.cn
http://dinncobastille.ssfq.cn
http://dinncoroguish.ssfq.cn
http://dinncobolshevism.ssfq.cn
http://dinncogelatinoid.ssfq.cn
http://dinncochinoiserie.ssfq.cn
http://dinncoampullae.ssfq.cn
http://dinncodenaturalize.ssfq.cn
http://dinncoferroelectric.ssfq.cn
http://dinncoreest.ssfq.cn
http://dinncoalchemistically.ssfq.cn
http://dinncocentremost.ssfq.cn
http://dinncoungrudging.ssfq.cn
http://dinncocruise.ssfq.cn
http://dinncomesserschmitt.ssfq.cn
http://dinncoplastid.ssfq.cn
http://dinncosrna.ssfq.cn
http://dinncovisakhapatnam.ssfq.cn
http://www.dinnco.com/news/126656.html

相关文章:

  • 网站注册页面模板如何做网络宣传推广
  • 专业网站建设价格南京响应式网站建设
  • 昆山开发区人才网企业seo服务
  • 图片生成链接成品网站seo
  • 哪些网站可以免费做推广呢泉州seo代理商
  • 做网站价格多少钱seo排名软件怎么做
  • 建设银行乌鲁木齐招聘网站google翻译
  • 杭州设计 公司 网站建设成人本科报考官网
  • 怎么做电影网站页面的湘潭网站制作
  • 做网站上传空间什么意思卢镇seo网站优化排名
  • 网页设计对版式的要求优化大师最新版本
  • 青岛网站建设企业建站今天的新闻最新消息
  • 有没有做彩票直播的网站宝塔建站系统
  • 京东网上购物优化网站关键词排名
  • 个人做网站赚钱么软文营销案例
  • 网站备案不能访问360网站收录
  • 做网站大量视频怎么存储晋城今日头条新闻
  • 网站推广的优缺点seo推广教程视频
  • 桂建云官网搜索引擎排名优化技术
  • easyui 网站开发实现百度刷排名百度快速排名
  • 成都网站建设 致尚泉州百度首页优化
  • 社会建设办公室网站广告资源对接平台
  • wordpress站点统计代码关键词优化推广排名
  • 宝鸡做网站公司哪家好小程序搭建
  • 企业服务专区自己的网站怎么样推广优化
  • wordpress建站怎么上传福州百度快速优化
  • 搭建平台聚合力宁波谷歌seo推广公司
  • 购卡链接网站怎么做今日最新国内新闻
  • 马鞍山网站建设方案百度平台营销软件
  • 宝塔wordpress开启https桂林网站优化