当前位置:网站首页>How to calculate the size of structure, segment and Consortium (common body)
How to calculate the size of structure, segment and Consortium (common body)
2022-07-28 06:48:00 【JuLiJuLi.】
How to calculate the size of the structure .
If you want to calculate the size of the structure , We must know the concept of memory alignment of structures , Here are some elements of memory alignment of structures :
1. The first structure member is offset from the structure variable by 0 The address of .
#include<stdio.h>
struct S1
{
char c1;//1 Bytes
int i;//4 Bytes
char c2;//1 Bytes
};
struct S2
{
char c1;//1 Bytes
char c2;//1 Bytes
int i;//4 Bytes
};
int main()
{
printf("%d\n", sizeof(struct S1));//12
printf("%d\n", sizeof(struct S2));//8
return 0;
}Let's see how we got this result ?

Later, we run the code to see whether it is consistent with our analysis ,

How to calculate the size of the bit segment .
#include<stdio.h>
struct A
{
int _a : 2;
int _b : 5;
int _c : 10;
int _d : 30;
};
int main()
{
printf("%d\n", sizeof(struct A));//8
return 0;
}If struct A If it's not a bit segment, we know 4 The size of a plastic is 16, How did this code get 8 What about the size ?
Let's first understand how the memory of the lower segment is allocated
1. The members of a segment can be int unsigned int signed int Or is it char ( It belongs to the plastic family ) Type of .

Let's run the code to verify , because Bit segments involve many uncertainties , Bit segments are not cross platform , Pay attention to portable program should avoid using bit segment .

Consortium ( Shared body ) How to calculate the size of .
#include<stdio.h>
union S1
{
char c;
int i;
};
int main()
{
union S1 s;
printf("%d\n", sizeof(s));//4
return 0;
}Let's first look at the joint memory calculation rules :
Memory computing rules :
1. The size of the union is at least the size of the largest member ,( Because the union must at least have space to save the largest member )


边栏推荐
猜你喜欢
![[c language] - step by step to achieve minesweeping games](/img/ee/49ddfcd948ccd5c8c9dec3c48c6112.png)
[c language] - step by step to achieve minesweeping games

rancher部署实战
![[explain in detail how to realize Sanzi chess step by step]](/img/17/68ef51ec2be0c86019461116ecaa82.png)
[explain in detail how to realize Sanzi chess step by step]

关于Shader KeyWord的整理
![[dynamic planning -- the best period for buying and selling stocks series 3]](/img/9f/f6c07264f5ffaa0fdfcc724c713e83.png)
[dynamic planning -- the best period for buying and selling stocks series 3]

mysql-8.0.17-winx64(附加navicat)手动配置版安装

SSAO By Computer Shader(一)

Everything you don't know about time complexity is here

Prometheus monitoring Nacos

prometheus监控nacos
随机推荐
Valgrind tool
OJ 1242 大一上之初出茅庐
Analysis of reentrantlock source code of AQS
[untitled]
Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
Ready to start blogging
rancher部署实战
About the collation of shader keyword
Mongodb quick start
Mongodb replica set and partitioned cluster
Bug experience related to IAP jump of stm32
网络通信及TCP/IP协议
准备开始写博客了
Development of Quantitative Trading Robot System
Implementation of simple address book in [c language]
Pyppeter drop-down selenium drop-down
单项链表的创建、遍历以及按要求查找结点
[realize the simple version of minesweeping games]
网络——网络层
OJ 1089 Spring Festival travel