当前位置:网站首页>C secret arts script Chapter 5 (paragraph) (Section 3)
C secret arts script Chapter 5 (paragraph) (Section 3)
2022-06-12 14:24:00 【Mortal programming biography】
Previous section , We learned about the memory alignment of structures , We found that the memory alignment of structures will lead to a waste of memory space , Is there any better way to reduce the waste of memory space ? The answer is yes : This is the new content we are going to learn today : Bit segment
. What is a bit segment
(1) The member of the segment must be int,unsigned int, signed int or char,unsigned char, signed char.
2. There is a colon and a number after the member name of the bit segment .( Basic format of bit segment )
3. Bit segment the bit here refers to the binary bit .
for example :
struct s
{
int a:2;
int b:5;
int c:10;
int d:30;
};
So let's use sizeof Operator to calculate the size of the structure , Is it normal structure memory alignment , Or is there another algorithm .
stay vs2013 The answer under the compiler is :8 Bytes .
How is it calculated ? The following is the memory layout :
therefore 4+4 Bytes =8 Bytes .
The operation here , First, we opened up in memory 4 Bytes (32 individual bit position ) Of memory space , Well int a:2(2 individual bit position ) Put it in , And then we parted int b:5(5 individual bit position ) and int c:10(10 individual bit position ) Put it in , such 32 Bit of memory space has been used 17 individual bit Yes. , Then we're going to int d:30(30 individual bit position ) Put in your data , But now only 15 individual bit Bit of memory space , So we have to waste space , Open up a new 4 Bytes (32bit position ) Of memory space , In the end, we opened up 8 Bytes of space .
Some Taoist friends have doubts when they see this , This is a waste of memory space ? There seems to be no difference with memory alignment . Try our memory alignment to calculate the size of this space , And finally 16 A byte , Our method today only uses half of the memory alignment , Beauty is not true ?
Be careful : This result is in vs2013 Results in the compiler environment , About other compiler environments , The bit segment cannot hold the stored contents in the remaining memory space , It is a waste of space to re open a space for storage , Or then fill up the remaining space , Depending on the compiler environment .
2. Bit segment memory allocation
(1) The space of bit segments is as needed 4 Bytes (int) perhaps 1 Bytes (char) The way to open up .
{2} Bit segments involve many uncertainties , Bit segments are not cross platform , Pay attention to portable program should avoid using bit segment .
(3)vs Compiler data is put from right to left , Other compilers are stored from left to right or from right to left, depending on the compiler .
Even if the bit segments look good , But there are still many defects .
3. The cross platform problem of bit segment
(1)int It's uncertain whether a bit segment is treated as a signed number or an unsigned number .
(2) The maximum number of bit segments cannot be determined ,(16 The machine is the largest 16),32 The machine is the largest 32, It's written in 27, stay 16 The machine will go wrong .
(3) The members in the bit segment are allocated from left to right in memory , Or right to left allocation criteria have not yet been defined .
(4) When a structure contains two bit segments , The second segment is relatively large , When the remaining bits of the first bit segment cannot be accommodated , Whether to discard the remaining bits or to use , This is uncertain .
(5) When a structure contains two bit segments , The second segment is relatively large , Cannot hold the remaining bits of the first bit segment , Is to abandon the rest
summary : Compared to the structure , Bit segments can really save space , But there are many cross platform problems with bit segments , Pay attention to portable program should avoid using bit segment .
Okay , Happy time is always so short , Do you think that a harvested Taoist friend can leave your praise ? Your praise is the motivation of bloggers . I'll see you in the next section !
边栏推荐
- Alicloud development board vscode development environment setup
- chrome://tracing Performance analysis artifact
- Sorting out the differences between ABS () and Fabs () in C language
- Programmer interview golden classic good question / interview question 01.05 Edit once
- [OCR] aspriseocr C # English, number recognition (not Chinese)
- Why do Chinese programmers change jobs?
- 拆改广告机---业余解压
- Player actual combat 23 decoding thread
- C語言中主函數調用另外一個函數,匯編代碼理解
- Create a slice slice pit using the make method
猜你喜欢

Redis核心配置和高级数据类型

C語言中主函數調用另外一個函數,匯編代碼理解

Axi4 increase burst / wrap burst/ fix burst and narrow transfer

Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge

PostgreSQL14安装使用教程

Player actual combat 25 unpacking module add close

浅谈中国程序员为什么要跳槽?

高考回忆录

Conversion of player's actual 10 pixel format and size

完美收官|详解 Go 分布式链路追踪实现原理
随机推荐
Analysis of two-dimensional array passing as function parameter (C language)
华为设备配置BGP AS号替换
My resume.
Wait function in SystemC
QT link error: undefined reference to VTable for "XXX“
Introduction to database system (Fifth Edition) notes Chapter 1 Introduction
Create a slice slice pit using the make method
Introduction to QT reflection mechanism and signal slot mechanism
Dial up and Ethernet
如果要打造品牌知名度,可以选择什么出价策略?
高考回憶錄
Démontage et modification de la machine publicitaire - décompression amateur
测试工程师如何转型测开
G++ error in compiling Win32 program: undefined reference to '__ imp_ GetStockObject‘
Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)
How to use Android studio to create an Alibaba cloud Internet of things app
Summary of virtual box usage problems
【MySQL】数据库基本操作
正点原子STM32F429核心板的插座型号
Crack WinRAR to ad pop-up window
