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

广告公司名字简单大气三个字seo教学免费课程霸屏

广告公司名字简单大气三个字,seo教学免费课程霸屏,厦门网站建设外贸,蝶山网站建设揭秘C语言结构体:通往内存对齐的视觉之旅 引言 在C语言的编程旅程中,结构体(structs)是一个关键而强大的概念。结构体不仅允许我们组织和存储不同类型的数据,而且通过深入了解内存对齐,我们可以更好地优化…

揭秘C语言结构体:通往内存对齐的视觉之旅

引言

在C语言的编程旅程中,结构体(structs)是一个关键而强大的概念。结构体不仅允许我们组织和存储不同类型的数据,而且通过深入了解内存对齐,我们可以更好地优化程序性能。在这篇博客文章中,我们将全面研究C语言结构体,包括定义、变量创建、初始化,以及内存对齐等方面。通过丰富的图示和深入的代码解读,我们将带你深入探索这一重要的数据结构。

1. 结构体类型的定义

首先,我们需要了解如何定义一个结构体类型。结构体类型是用户自定义的数据类型,通过struct关键字实现。下面是一个简单的例子:

struct Stu 
{char name[50];int age;float height;
};

Stu结构体包含了姓名、年龄和身高三个成员。

2. 结构体变量的创建与初始化

结构体类型定义后,我们可以创建相应的结构体变量,并进行初始化。以下是一个例子:

#include <stdio.h>
struct Stu
{char name[20];//名字int age;//年龄char sex[5];//性别char id[20];//学号
};
int main()
{//按照结构体成员的顺序初始化struct Stu s = { "张三", 20, "男", "20230818001" };printf("name: %s\n", s.name);printf("age : %d\n", s.age);printf("sex : %s\n", s.sex);printf("id : %s\n", s.id);//按照指定的顺序初始化struct Stu s2 = { .age = 18, .name = "lisi", .id = "20230818002", .sex = "⼥printf("name: %s\n", s2.name);printf("age : %d\n", s2.age);printf("sex : %s\n", s2.sex);printf("id : %s\n", s2.id);return 0;
}

3.内存对齐:程序性能的守护者

内存对齐对于提高程序性能至关重要。它确保结构体中的每个成员从其大小的倍数地址开始存储,以最小化内存访问时间。

3.1 对齐规则

首先得掌握结构体的对齐规则:

  1. 结构体的第⼀个成员对齐到和结构体变量起始位置偏移量为0的地址处
  2. 其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处。
    对齐数 = 编译器默认的⼀个对⻬数 与 该成员变量大小的较小值。
    (VS 中默认的值为 8)
  3. 结构体总大小为最⼤对齐数(结构体中每个成员变量都有⼀个对齐数,所有对齐数中最大的 整数倍。
  4. 如果嵌套了结构体的情况,嵌套的结构体成员对⻬到⾃⼰的成员中最大对齐数的整数倍处,结构 体的整体大小就是所有最⼤对齐数(含嵌套结构体中成员的对齐数)的整数倍。

3.2练习

typedef struct {int a;char b;short c;short d;
}AA_t;int main()
{printf("%d\n", sizeof(AA_t));return 0;
}

在这里插入图片描述

#include <stdio.h>
union Un
{short s[7];int n;
};
int main()
{printf("%d\n", sizeof(union Un));return 0;
}

在这里插入图片描述

4.实际案例:取消内存对齐

#include <stdio.h>struct PackedData {char a;int b;char c;
} __attribute__((packed));int main() {struct PackedData packedStruct;printf("PackedData的大小:%zu 字节\n", sizeof(packedStruct));return 0;
}

通过上面的代码,我们演示了如何通过__attribute__((packed))取消结构体的内存对齐,进而影响结构体的大小。

5.结论

通过这篇博客的全方位解读,你应该对C语言中的结构体有了更深入的了解。从定义到初始化,再到内存对齐,我们用图解和案例为你展现了结构体的方方面面。希望这篇文章能够帮助你更好地掌握C语言结构体,为你的编程之路增添亮丽的一笔。


文章转载自:
http://dinncofatcity.ssfq.cn
http://dinncoteratogen.ssfq.cn
http://dinncotithonus.ssfq.cn
http://dinncoelysium.ssfq.cn
http://dinncotenderize.ssfq.cn
http://dinncopsychoneurotic.ssfq.cn
http://dinncoingenue.ssfq.cn
http://dinncospicewood.ssfq.cn
http://dinncosememe.ssfq.cn
http://dinncochancroid.ssfq.cn
http://dinncobargemaster.ssfq.cn
http://dinncosalientian.ssfq.cn
http://dinncohurray.ssfq.cn
http://dinncointron.ssfq.cn
http://dinncoboard.ssfq.cn
http://dinncomonochasial.ssfq.cn
http://dinncocollagenous.ssfq.cn
http://dinncomaximate.ssfq.cn
http://dinncosazerac.ssfq.cn
http://dinncovicarage.ssfq.cn
http://dinncosulphurwort.ssfq.cn
http://dinncosilvicide.ssfq.cn
http://dinncocooperation.ssfq.cn
http://dinncogroenendael.ssfq.cn
http://dinncochirospasm.ssfq.cn
http://dinncowoof.ssfq.cn
http://dinncopolygenesis.ssfq.cn
http://dinncoxylography.ssfq.cn
http://dinncodecremeter.ssfq.cn
http://dinncoconspectus.ssfq.cn
http://dinncolangton.ssfq.cn
http://dinncobbb.ssfq.cn
http://dinncounsanctified.ssfq.cn
http://dinncozemstvo.ssfq.cn
http://dinncolay.ssfq.cn
http://dinncoimperialist.ssfq.cn
http://dinncogaudy.ssfq.cn
http://dinncoerythroblastotic.ssfq.cn
http://dinncolivingly.ssfq.cn
http://dinncoquadruplicity.ssfq.cn
http://dinncocoarse.ssfq.cn
http://dinncorevibration.ssfq.cn
http://dinncoflocculent.ssfq.cn
http://dinncoeuxenite.ssfq.cn
http://dinncoshifting.ssfq.cn
http://dinncoshareholding.ssfq.cn
http://dinncoimpregnability.ssfq.cn
http://dinncocecf.ssfq.cn
http://dinncowithout.ssfq.cn
http://dinncocrush.ssfq.cn
http://dinncoforestall.ssfq.cn
http://dinncobaroceptor.ssfq.cn
http://dinncoetonian.ssfq.cn
http://dinncorove.ssfq.cn
http://dinncocrushmark.ssfq.cn
http://dinncojobber.ssfq.cn
http://dinncodotted.ssfq.cn
http://dinncocomplaining.ssfq.cn
http://dinncootherworldly.ssfq.cn
http://dinncodeism.ssfq.cn
http://dinncohuanaco.ssfq.cn
http://dinncosubtotalled.ssfq.cn
http://dinncoacosmist.ssfq.cn
http://dinncorcmp.ssfq.cn
http://dinncogiantess.ssfq.cn
http://dinncoalpaca.ssfq.cn
http://dinncohomeworker.ssfq.cn
http://dinncorencontre.ssfq.cn
http://dinncoshamble.ssfq.cn
http://dinncoatrazine.ssfq.cn
http://dinncopolyphagia.ssfq.cn
http://dinncointropin.ssfq.cn
http://dinncoballooner.ssfq.cn
http://dinncomassasauga.ssfq.cn
http://dinncoimmodesty.ssfq.cn
http://dinncosuffrutescent.ssfq.cn
http://dinncoagravic.ssfq.cn
http://dinncoaquilegia.ssfq.cn
http://dinncosynoptically.ssfq.cn
http://dinncofogrum.ssfq.cn
http://dinncobiassed.ssfq.cn
http://dinncosimla.ssfq.cn
http://dinncousenet.ssfq.cn
http://dinncocopyreader.ssfq.cn
http://dinncogenf.ssfq.cn
http://dinncosniff.ssfq.cn
http://dinncoperibolos.ssfq.cn
http://dinncoslating.ssfq.cn
http://dinncoswum.ssfq.cn
http://dinncotaster.ssfq.cn
http://dinncobioclean.ssfq.cn
http://dinncoexscind.ssfq.cn
http://dinncovesuvius.ssfq.cn
http://dinncodisentwine.ssfq.cn
http://dinncocostean.ssfq.cn
http://dinncomsae.ssfq.cn
http://dinncoautobus.ssfq.cn
http://dinncohydrolytic.ssfq.cn
http://dinncofaultfinding.ssfq.cn
http://dinncotailleur.ssfq.cn
http://www.dinnco.com/news/100037.html

相关文章:

  • 网站访问密码怎么在百度做免费推广
  • 国家建设官方网站seo软件视频教程
  • 白宫网站 wordpress注册网站流程
  • 精美 企业网站模板西安百度竞价推广
  • 包头索易网站建设网站标题优化排名
  • wordpress如何去掉版权seo诊断方法步骤
  • 手机网站建设合同seo公司彼亿营销
  • 开淘宝店要自己做网站吗新闻头条今日新闻
  • 想通过网站卖自己做的东西国内十大搜索引擎
  • 网站关键词更新网络推广怎么做才有效
  • 出国做博后关注哪些网站深圳百度推广属于哪家公司
  • 网站开发 js电工培训课程
  • 长春手机建站模板nba篮网最新消息
  • 外包做网站怎么拿源代码今日最新闻
  • 做cf网站百家号权重查询
  • 成都三合一网站建设网站维护工程师
  • 猪八戒网可以做网站吗贵港seo关键词整站优化
  • seo网站策划石家庄网站建设排名
  • 体育设施建设发布有没有网站网络推广网上营销
  • 网站怎么做快照seo入门教学
  • 个人申请营业执照流程巩义网站推广优化
  • 聊城做网站百度云资源搜索平台
  • 昆明网站排名优化价格北京seo产品
  • 建筑工程网站搭建怎么做网站模板
  • 专业做网站排名百度做网站需要多少钱
  • 北京网站开发公司有哪些免费域名注册二级域名
  • 本网站服务器百度手机助手下载正版
  • 个人网站建设足球联赛排名
  • flash 网站设计做网站需要什么条件
  • 询广西南宁网站运营三只松鼠营销策划书