当前位置:网站首页>How much space does structure variable occupy in C language
How much space does structure variable occupy in C language
2022-06-24 03:56:00 【Good pie notes】
I wanted to record the summary of memory alignment for a long time , But I have very very Serious procrastination , It was not until today that I made up my mind to settle this matter , No more nonsense , starts !(ps: The compiler I used version by gcc Ubuntu4.9.2-10 ubuntu13 4.9.2)
How much space does the structure occupy ? Let's take a look at the output of the following question :
#include<stdio.h>
typedef struct test
{
char a;
int b;
double c;
}TEST;
int main(void)
{
TEST test1;
printf("%ld\n",sizeof(test1));
return 0;
} Dare to guess , Is the space occupied by a structure the algebraic sum of the space occupied by its members ?? According to this conjecture , The answer to this question should be 13, However, the fact is surprising , The output of this question is 16.google This is because there is a mechanism called memory alignment in the computer, which leads to this result .
In the computer, we usually let CPU Read several bytes of data from memory at a time , Instead of reading one byte of data at a time , The advantage of this is to improve the efficiency of the computer , However, the disadvantages are also obvious .
hypothesis CPU Read four bytes of data from memory at a time , Now there is a in memory char Type data and a int Data of type , If the memory is not aligned , When CPU For the first time, across four byte addressing, a char Data of type , And then CPU Point to int The middle area of type , Lead to this int Type variable not found , then CPU Will return to find again , Until we find it int Type variable . This not only failed to improve efficiency , It increases CPU The burden of . So we usually do it first char Type variable is filled with some data to ensure that the address is an integer multiple of the data each time it is addressed , This avoids the above “ error ” Happen , This is called memory alignment .
The rules for memory alignment are simple :
One 、 The starting position is an integer multiple of the memory occupied by the member data type , If it is insufficient, the insufficient part fills the memory with data to an integer multiple of the data type .
Two 、 The total memory occupied by a structure is an integer multiple of the largest data type occupied in its member variables .
Suppose the structural variables in the above question are stored from memory No. 0 , be char Type variable takes one byte , Then int The start position of the type variable is found in memory 1 , It does not satisfy that the starting position is int Type 4 Requirements for byte integer multiples , Therefore, the memory number one, two and three is filled up , Store this... From memory number four int Type members , When it's time to int Eight bytes of space have been used after the completion of the type member storage , So now double The starting position of the type member is memory number eight , The first condition is met , therefore double Type start storage , After the storage is completed, the structure variable just accounts for 16 Bytes , Just the largest data type double An integer multiple of eight bytes , So the storage is complete , Therefore, the structural variable accounts for 16 Bytes .
边栏推荐
- A Tencent interview question
- hprofStringCache
- Rasa 3. X learning series -rasa 3.2.0 new release
- A figure showing the price and expense structure of Tencent cloud real-time audio and video TRTC
- Understand Devops from the perspective of leader
- flutter系列之:flutter中的offstage
- 元气森林推“有矿”,农夫山泉们跟着“卷”?
- 浅谈游戏安全 (一)
- After 20 years of development, is im still standing still?
- Slide left from small window to large windowdispatchframelayout
猜你喜欢

ModStartCMS 主题入门开发教程

你了解TLS协议吗?
Thank you for your recognition! One thank-you note after another

【代码随想录-动态规划】T392.判断子序列

Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design

元气森林推“有矿”,农夫山泉们跟着“卷”?

618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?

Black hat SEO actual combat directory wheel chain generates millions of pages in batch

618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?
![[numpy] numpy's judgment on Nan value](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[numpy] numpy's judgment on Nan value
随机推荐
Modstartcms theme introductory development tutorial
Rasa 3.x 学习系列-Rasa 3.2.0 新版本发布
Getlocationinwindow source code
web渗透测试----5、暴力破解漏洞--(7)MYSQL密码破解
[Tencent cloud update] against 11.11! Here comes the 1.1% discount for enterprises!
Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
Common content of pine script script
内存泄漏之KOOM
Prometheus PushGateway 碎碎念
Technical dry goods - how to use AI technology to accurately identify mining Trojans
Iguard6.0 - appropriate website protection system
[congratulations] rock solid! A new generation of AMD Blackstone architecture instance is launched!
Prometheus pushgateway
618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
How to save pictures to CDN? What are the advantages of this?
Understand Devops from the perspective of leader
well! Do you want to have a romantic date with the shining "China Star"?
Pine Script脚本常用内容
Idea 1 of SQL injection bypassing the security dog
Self built DNS to realize the automatic intranet resolution of tke cluster apiserver domain name