当前位置:网站首页>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
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 !
边栏推荐
- LC173. 二叉搜索树迭代器
- Xshell configuration xforward forwarding Firefox browser
- PMP project integration management
- Stop slave is stuck -- the event of the transaction is not copied completely
- [leetcode] most elements [169]
- 设置单击右键可以选择用VS Code打开文件
- Learning Websites commonly used by circuit designers
- 海思调用接口之Makefile配置
- 地平线2022年4月最新方案介绍
- pytorch训练CPU占用持续增长(bug)
猜你喜欢
[Yangcheng cup 2020] easyphp
P1007 single log bridge
【喜欢的诗词】好了歌
LeetCode 968. Monitor binary tree
Introduction to the latest plan of horizon in April 2022
Chow-Liu Tree
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
PMP project integration management
`Usage of ${}`
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
随机推荐
[chestnut sugar GIS] ArcScene - how to make elevation map with height
boot actuator - prometheus使用
剑指 Offer II 099. 最小路径之和-双百代码
密码技术---分组密码的模式
Construction of Hisilicon 3559 universal platform: rotation operation on the captured YUV image
容器化技术在嵌入式领域的应用
阿里云有奖体验:如何使用 PolarDB-X
C#中Linq用法汇集
LC173. 二叉搜索树迭代器
Generics and reflection, this is enough
Chow-Liu Tree
[leetcode] reverse string [344]
Learning records of data analysis (II) -- simple use of response surface method and design expert
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
BBR 遭遇 CUBIC
Boot actuator - Prometheus use
Cryptographic technology -- key and ssl/tls
easyclick,ec权朗网络验证源码
Freshman learning sharing
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022