Dynamic memory allocation
Overview of data types
Memory allocation
1 Shaping memory allocation
- Original code:
directly translate binary into binary in the form of positive and negative numbers. Wherein the highest bit is the sign bit positive number of 0 to 1 negative . - Inverse code:
Leave the sign bit of the original code unchanged , and the other bits can be obtained by inverting them bit by bit. - Complement code: the complement code
+1 is obtained.
Note: All operations are performed on complements because computer operations are performed on complements in memory
2 floating point number memory allocation
Look at this example first. In this example
, you can see that the value of the integer 9 is changed drastically by reading the floating point number. Similarly, the 9 of the floating point number is read by the integer reading. A similar situation occurs.
It can be seen that the storage method of floating-point numbers is different from the storage method of integers.
According to the international standard IEEE (Institute of Electrical and Electronic Engineering) 754, any binary floating-point number V can be expressed in the following form:
- (-1)^S * M * 2^E
- (-1)^s represents the sign bit, when s=0, V is a positive number; when s=1, V is a negative number.
- M represents a significant number, greater than or equal to 1, less than 2
- 2^E represents the exponent bit
For example: 5.0 in decimal, 101.0 in binary, equivalent to 1.01×2^2. Then, according to the format of V above, s=0,
M=1.01, and E=2 can be obtained .
Decimal -5.0, written in binary is -101.0, which is equivalent to -1.01×2^2. Then, s=1, M=1.01, and E=2.
Attention ! ! ! !
- ^ Here means how many powers
- Not to be confused with decimal, where 1.01 is a binary number every time it is multiplied by 2 and the decimal point is one digit behind.
- When saving M inside the computer, the first digit of this number is always 1 by default, so it can be discarded and only the xxxxxx part after it is saved .
For example, when saving 1.01, only 01 is saved, and when it is read, the first 1 is added. The purpose of this is to save 1 significant figure.
Take a 32-bit floating-point number as an example. There are only 23 bits left for M. After the 1 in the first bit is rounded down, it is equal to
the special case that can store 24 significant digits E.
First, E is an unsigned int (unsigned int). This means that if E is 8 bits, its value range is 0~255; if E is 11 bits, its value range is 0~2047. However, we know that, in the scientific notation E is a negative number, the IEEE 754 provides real time into memory E
real-valued intermediate must be combined with a number, for an 8-bit E, the middle number is 127 ; For an 11-digit E, the middle number is 1023. For example, the E
of 2^10 is 10, so when saving as a 32-bit floating point number, it must be saved as 10+127=137, which is 10001001.
The exponent E is taken out of the memory and can be further divided into three situations: - When E is not all 0 or not all 1
, the floating-point number is expressed by the following rule, that is, the calculated value of exponent E is subtracted from 127 (or 1023) to get the true value, and then the effective number M is
added before the first digit 1. For example: the binary form of 0.5 (1/2) is 0.1, because the positive part must be 1, that is, the decimal point is shifted to the right by 1 place,
then it is 1.0*2^(-1), and its order code is -1+127= 126 is represented as 01111110, and the mantissa 1.0 removes the integer part to 0, and fills in 0 to 23 bits
00000000000000000000000, then its binary representation is: - E is all 0.
At this time, the exponent E of the floating-point number is equal to 1-127 (or 1-10223), which is the true value. The effective digit M is no longer added to the first digit 1, but is reduced to
a decimal of 0.xxxxxx. This is done to represent ±0, and very small numbers close to zero. - E is all 1 at
this time, if the significant digits M are all 0, it means ± infinity (positive and negative depends on the sign bit s);
now we are looking back at the example problem.
First, we first get the 9's. The
same can be drawn
3 Judgment of the big and small
Definition :
-
Big-endian storage: the low-endian content of a number is stored at the high address, and the high-endian content is stored at the low address.
-
Little-endian storage: the low-endian content of a number is stored at the low address, and the high-endian content is stored at the high address.
High low
We have observed that
our vs2019 storage is little-endian storage.
How to design code to distinguish storage methods?
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int Differentiate(i)
{
return *(char*)&i;
}
int main()
{
int a = 0x01;
int m = Differentiate(a);
if (m == 1)
{
printf("小端");
}
else
printf("大端");
return 0;
}