当前位置:网站首页>Deep analysis of data storage in memory - C language
Deep analysis of data storage in memory - C language
2022-07-02 23:09:00 【hania_ w】
List of articles
1. Introduction to data types
1. Basic classification of types
Before writing an introduction to data types , Let's first briefly introduce release Version and debug Memory differences between versions :
Let's put the following code in VS I'm gonna run it in , The results are quite different
int i = 0;
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
for (i = 0; i <= 12; i++)
{
arr[i] = 0;
printf("hehe\n");
}
Put this code in debug The result under version is hehe Dead cycle , As shown in the figure below
You can see it here ,hehe It's in an endless cycle
Put this code in Release The result under version is 13 individual hehe
The fundamental reason is that under these two versions , Data is stored in different ways
The following is a diagram :
The above figure shows the difference between the two , When the compiler goes from low address to high address ,debug In the environment ,arr At the end of the array, if you continue to run down, it will change i Value , send i The value of is initialized to 0.release In the environment arr The end of the array does not change i Value , Therefore, it will not fall into a dead cycle .
next , recall c Basic types of data in language :
1、 Plastic surgery Family
char
notes : The essence of character types is ASCII Code value , It's plastic surgery , Therefore, it is divided into plastic surgery family .
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
except char type , Other types of data without specific instructions , The default is There are sign types .char The type depends on the compiler .
2、 Floating point family
float Low precision , The range of stored values is small
double High precision , The range of stored values is larger
3、 Construction type ( Custom type , We can create new types )
An array type
Type of structure struct
Enumeration type enum
Joint type union
4、 Pointer types
int *pi
char *pc
float* pf
void* pv
5、 Empty type
1、void Indicates empty type ( No type )
2、 Usually applied to the return type of a function 、 The parameters of the function 、 Pointer types
See the following code example
void test(void)
{
// first void Indicates that the function has no return value
// the second void Indicates that the function does not require any arguments
printf("hehe\n");
}
int main()
{
test(1);
return 0;
}
2、 Shaping storage in memory
1、 Source code 、 Inverse code 、 Complement code
There are three ways to represent integers in a computer , The original code 、 Inverse and complement
The three representations have two parts: sign bit and numeric bit , The sign bits are
use 0 Express “ just ”, use 1 Express “ negative ”, And three kinds of numeric bit negative integers
The expression methods are different .
Original code : Directly translate binary into binary in the form of positive and negative numbers Just make it .
Inverse code : Change the sign bit of the original code , The other bits can be inverted in turn .
Complement code : Inverse code +1 You get the complement .
The original code of a positive number 、 Inverse code 、 The complement is the same , For plastic surgery : The data stored in memory is actually Complement code
We won't explain the specific reasons here .
2、 About the concept of size end
What is the big and small end ? In fact, it is the storage mode of data in memory , Large end storage mode and small end storage mode .
Big end ( Storage ) Pattern : The low bit of data is stored in the high address of memory , And the high end of the data , Low address saved in memory
in ;
The small end ( Storage ) Pattern : The low bit of data is stored in the low address of memory , And the high end of the data ,, Stored in a high address in memory .
Now let's look at a topic : Determine whether the storage mode of the current machine is big end storage or small end storage .
#include <stdio.h>
int check_sys()
{
int i = 1;
return (*(char*)&i);
// See the following figure for specific reasons
}
int main()
{
int ret = check_sys();
if (ret == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}

3、 Floating point storage in memory
( The focus of this blog )
1、
(-1)^S * M * 2^E
(-1)^s The sign bit , When s=0,V Is a positive number ; When s=1,V It's a negative number .
M Represents a significant number , Greater than or equal to 1, Less than 2.
2^E Indicates the index bit .
for example V=5.0 : Floating point numbers are stored as 101.0](/img/36/e4b92cee7e7810cd504d2b7e44a04f.png)
If we want to figure out S、M、E, Then there can only be one decimal place
example : V=9.5=1001.1=1.0011*2^3
therefore S=0,M=1.0011,E=3
however , There are exceptions to everything , therefore , Not all floating-point numbers can be represented in this way
for example :V=9.6=1001.10… And 1001.11 Hover between , It cannot be expressed accurately
float —> 4byte —>32bit
double—>8byte—>64bit
although double Type ratio float The accuracy of the type should be large , But they still may not be able to save the decimal memory completely .
2、 It is worth noting that floating point numbers are used in memory S、M、E In the form of
about 32 Floating point number of bits , The highest 1 Bits are sign bits s,
And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M.

about 64 Floating point number of bits , The highest 1 Bits are sign bits S,
And then 11 Bits are exponents E, The rest 52 Bits are significant numbers M

3、 Index E Is a complex number
First E Is an unsigned integer, which means , If E by 8 position , Its value range is 0 ~ 255; If E by 11 position , Its value range is 0~2047. however , We know , In scientific counting E You can have negative numbers , therefore , In memory E The true value of must be added with an intermediate number , about 8 Bit E, The middle number is 127; about 11 Bit E, The middle number is 1023. such as ,2^10 Of E yes 10, So save it as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.
If there is an error , I hope you can make comments or private letters !
边栏推荐
- MySQL查询附近的数据.并按距离进行排序.
- Go语言sqlx库操作SQLite3数据库增删改查
- 海思3559万能平台搭建:在截获的YUV图像上旋转操作
- 从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
- Kubernetes uses the host name to allocate the pod on the specified node
- 深度剖析数据在内存中的存储----C语言篇
- ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
- The kth largest element in the [leetcode] array [215]
- 1px pixel compatibility of mobile terminal, 1px border
- Webrtc audio and video capture and playback examples and mediastream media stream analysis
猜你喜欢

Use of recyclerview with viewbinding

mysql重置密码,忘记密码,重置root密码,重置mysql密码

【喜欢的诗词】好了歌

Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs

數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
![[npuctf2020]ezlogin XPath injection](/img/6e/dac4dfa0970829775084bada740542.png)
[npuctf2020]ezlogin XPath injection

Splunk audit 的设定
![The kth largest element in the [leetcode] array [215]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
The kth largest element in the [leetcode] array [215]

xshell配置xforward转发火狐浏览器

数据标注典型案例,景联文科技如何助力企业搭建数据方案
随机推荐
數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
Qt QProgressBar详解
China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!
Potplayer set minimized shortcut keys
容器化技术在嵌入式领域的应用
LeetCode 968. 监控二叉树
海思 VI接入视频流程
Array advanced improvement
从2022年Q1财报看携程的韧性和远景
[leetcode] there are duplicate elements [217]
Realize the linkage between bottomnavigationview and navigation
泛型与反射,看这篇就够了
2016. 增量元素之间的最大差值
boot actuator - prometheus使用
The kth largest element in the [leetcode] array [215]
STM32串口DAM接收253字节就死机原因排查
Lc173. Binary search tree iterator
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
[Yangcheng cup 2020] easyphp
剑指 Offer II 099. 最小路径之和-双百代码