当前位置:网站首页>[Zhang San learns C language] - deeply understand data storage
[Zhang San learns C language] - deeply understand data storage
2022-07-02 06:47:00 【Xiexishan】
【 Zhang Sanxue C Language 】— Deep understanding of data storage
One , data type
Different data types represent the size of space opened up by different data in memory
eg:int In memory 4 Bytes
whole type | Content Be careful : The contents in brackets are the number of bytes occupied by each type |
---|---|
data type | char(1 byte ) short(2 byte ) int(4 byte ) long(8 byte ) |
floating-point | float (8 byte ) double(16 byte ) |
Construction type | An array type ( The memory size is decided by yourself ) Structure struct ( Observe memory alignment ) enumeration enum( Memory elements determine ) Joint type union( The largest element ) |
Pointer types | void Indicates the type , Usually used for the return type of a function , The parameters of the function , Pointer types |
C Language has no string type , So use arrays to store strings eg:char[10]="abcdef"
Two . The storage of integer data in memory
1. Source code , Inverse code , Complement code
The symbols in the computer have been stored in memory in binary form
Data is stored in memory in the form of complement
The first is the sign bit , The remaining bits are data bits
0 Being positive ,1 Negative
Source code : Direct binary can be translated into binary in the form of positive and negative numbers
Inverse code : The sign bits remain the same , The data position is reversed
Complement code : Inverse code +1
Positive numbers : Source code = Inverse code = Complement code
negative : Store... As a complement
eg:
Positive numbers
date:10
bin:00000000 00000000 00000000 00001010
Source code = Inverse code = Complement code =00000000 00000000 00000000 00001010
negative
date:-10
bin: 10000000 00000000 00000000 00001010
Inverse code :11111111 11111111 11111111 11110101
Complement code :11111111 11111111 11111111 11110110
2. The reason why data is stored in the form of complement in memory
a. The use of complement can unify the symbol bit and data field
b.CPU Only adders , Using complement can make addition and subtraction unified
3, data storage
Byte order :
Concept :cpu The order in which data in memory is accessed in bytes
Because there are high and low addresses in memory , There are high and low binary data
The host byte order mainly depends on cpu framework : X86- The small end ; MIPS- Big end
Byte order is mainly used in network communication , Communication between different hosts , If the byte order is different, it will cause data ambiguity
classification :int=0x11223344
Big end storage : The low address stores the high address Storage in memory 11 22 33 44
Small end storage : The low address stores the low address Storage in memory 44 33 22 11
notes : The size end is a high-frequency examination site in interviews and examinations , To learn more, click the link below to learn .
3、 ... and . The storage of floating-point data in memory
1,IEEE754 The standard document stipulates , The representation of any floating-point number in memory is divided into three parts
Symbol | Memory occupied by each bit of single precision floating-point numbers | Memory occupied by each bit of double precision floating-point numbers |
---|---|---|
s— Sign bit | 1 A bit | 1 A bit |
E— Exponential position | 8 A bit | 11 A bit |
M— mantissa | 23 A bit | 52 A bit |
Floating point numbers are expressed in exponential form in the computer , Decompose into : Numeral character , mantissa , Exponential sign , Index Four parts .
The number symbol accounts for 1 Bit binary , The positive and negative of a number .0 Express positive ,1 Negative
Exponential operators account for 1 Bit binary , The positive and negative of the index .
The mantissa represents the significant number of floating-point numbers ,0.xxxxxxx, But don't save the beginning 0 Sum point .
The effective number of the index .
How many places does the index occupy , How many bits does the mantissa occupy , It's up to the computer system to decide .
Two special cases :
E For all 0: At this time, the real index value is 1-127=-126,M Represent true value , Usually used to indicate positive and negative 0
E For all 1: At this time, the real index value is 255-127=128,M For all 0 Hour means positive and negative infinity
2. Value range
type | Number of bits | Significant figures | Range of values |
---|---|---|---|
float | 32 | 6-7 | -3.410(-38)~3.410(38) |
double | 64 | 15-16 | -1.710(-308)~1.710(308) |
long double | 128 | 18-19 | -1.210(-4932)~1.210(4932) |
If storage is more important than accuracy , Consider using float type . contrary , If accuracy is the most important condition , Then use double type .
Floating point variables can be promoted to larger Radix types ( from float Type to double type ). When you perform arithmetic on floating-point variables , Usually there will be an improvement . This arithmetic is always performed with the same precision as the variable with the highest precision .
float f_short;
double f_long;
long double f_longer;
gf_short = f_short * f_long
In the previous example , Variable f_short Promote to type double And with f_long Multiply ; then , Result rounded to type float, Then give to f_short.
3. Application example analysis
stay C and C++ in , The following assignment statement :
float a=0.1;
Compiler error :warning C4305: ‘initializing’ : truncation from 'const double ’ to 'float ’
reason : stay C/C++ in , To the right of the equal sign 0.1, Think it's a float, But the compiler thinks it's a double( Because decimals default to double), So I have to report this warning, It's usually changed to 0.1f It's all right. .
Welcome to exchange , If it works , Remember to comment + Collection + Pay attention to ! meow ~~
Today's rubbish : I didn't understand until I grew up , Nothing is difficult in the world , I'm just afraid that those who have a mind .
边栏推荐
- Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
- 微信小程序基础
- Virtualenv and pipenv installation
- Redis - cluster data distribution algorithm & hash slot
- Thread hierarchy in CUDA
- js中map和forEach的用法
- Win电脑截图黑屏解决办法
- Warp matrix functions in CUDA
- The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem
- AWD learning
猜你喜欢
Redis - big key problem
Sublime text configuring PHP compilation environment
Warp shuffle in CUDA
flex九宫格布局
Latex warning: citation "*****" on page y undefined on input line*
Utilisation de la carte et de foreach dans JS
Linux MySQL 5.6.51 Community Generic 安装教程
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
AWD learning
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
随机推荐
20210306转载如何使TextEdit有背景图片
2020-9-23 use of QT timer qtimer class.
pytest(3)parametrize参数化
Sentry construction and use
js中map和forEach的用法
Utilisation de la carte et de foreach dans JS
Detailed definition of tensorrt data format
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
ctf-web之练习赛
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
Pytest (3) parameterize
ctf三计
uniapp引入本地字体
记录一次RDS故障排除--RDS容量徒增
Kotlin - verify whether the time format is yyyy MM DD hh:mm:ss
Latex compilation error I found no \bibstyle &\bibdata &\citation command
The table component specifies the concatenation parallel method
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
After reading useful blogs
Kali latest update Guide