当前位置:网站首页>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 !
边栏推荐
- Value sequence < detailed explanation of daily question >
- Chow-Liu Tree
- odoo13搭建医院HRP环境(详细步骤)
- Cryptographic technology -- key and ssl/tls
- Jericho's thimble reaction when directly touching the prototype is abnormal [chapter]
- 详解Promise使用
- Qt QSplitter拆分器
- Odoo13 build a hospital HRP environment (detailed steps)
- STM32串口DAM接收253字节就死机原因排查
- Prometheus deployment
猜你喜欢
随机推荐
力扣刷题(2022-6-28)
Loss function~
20220524_数据库过程_语句留档
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
Minimum spanning tree
Odoo13 build a hospital HRP environment (detailed steps)
odoo13搭建医院HRP环境(详细步骤)
PotPlayer设置最小化的快捷键
QT qsplitter splitter
Qt QProgressBar详解
数据标注典型案例,景联文科技如何助力企业搭建数据方案
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
[leetcode] reverse string [344]
Successfully changed Splunk default URL root path
[hardware] origin of standard resistance value
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
泛型与反射,看这篇就够了
静态文件显示问题
剑指 Offer II 099. 最小路径之和-双百代码
antd组件upload上传xlsx文件,并读取文件内容