Detailed explanation of specific allocation of C/C++ data types from 0 to memory

Table of contents

1. Data type classification

Second, type memory size

Third, the specific storage method in memory (talk about the plastic family and the floating-point family, look forward to my next article about custom types)

1. The plastic family takes int as an example. First of all, we need to judge the size of the machine.

 2. The floating-point family takes float as an example.

Four: Test the sword with questions


1. Data type classification

1. Shaping family: char, short, int, long, long long, unsigned int, unsigned char, unsigned short, unsigned long, unsigned long long. (Why char is included in the integer family is because characters are stored in Ascll code values ​​in the machine)

2. Floating point family: float, double.

3. Custom types: struct, enum, union.

4. void type.

Second, type memory size

char/unsigned char (one byte)

short/unsigned short (two bytes)

int/unsigned int (four bytes)

long int/unsigned long int (greater than or equal to four bytes, depending on the machine)

long long/unsigned long long (greater than or equal to eight bytes)

float (four bytes)

duoble (eight bytes)

Third, the specific storage method in memory (talk about the plastic family and the floating-point family, look forward to my next article about custom types)

1. The plastic family takes int as an example. First of all, we need to judge the size of the machine.

Supplementing the machine endianness: Suppose we now define an int a = 1; its memory size is four bytes, the memory has an address number, in bytes, the two's complement of a is (the original complement can be See my previous article https://mp.csdn.net/mp_blog/creation/editor/130656985 ), 00000000000000000000000000000001, but the address is divided into high and low addresses. If the 1 in the complement is at the low address, the machine is little endian, and vice versa. It is big endian.

 Let's start with the use of int memory space. One byte is eight bits, so int has 32 bits. Let's look at the picture to understand its allocation (taking a little-endian machine as an example)

The storage of other plastic families is similar, only the memory size is different.

 2. The floating-point family takes float as an example.

First of all, we need to clarify one point. The storage of floating-point and integer in memory is completely different. Next, we will use float as an example to explain. First of all, any type of theoretical knowledge can be converted into scientific notation, such as 15 = 1.5*10^1. Our floating-point family is also stored in a similar way to scientific notation. First of all, it is still the sign bit. Its sign bit is the same as the integer. Different, it defaults to (-1)^s, if s is 0, it means it is a positive number, if it is 1, it is a negative number (just remember for now, there will be a picture later). Then store the part 1.5, but convert it to binary, that is, convert it to only 0 and 1. When storing, it will automatically omit 1, that is, only store the decimal part. When it is taken out, it will automatically add 1, and then The index part is stored. It is also converted into binary first, but when storing, it will add 127 and then convert it into binary storage. If you are interested, you can find relevant information by yourself. I won’t explain too much here. When the index part is taken out There are three possibilities: 1) After the first binary is stored, all 0s are added, because 127 is added during storage. 0 appears at this time, which means it is a very small number. When taking out all the numbers, the system will omit the previous ones. The 1 is directly in the form of 0.00000... * 2^ -127, 2) The exponent part is all 1, which means it is a very large number, which can be explored by yourself. 3) It is neither all 0 nor all 1, that is to add 127 and subtract it, and keep the others unchanged. The specific details are as follows

Four: Test the sword with questions

 View the question answer results:

9

0.000000

1091765616

9.000000

The reason is that the storage methods of floating point numbers and integers are different, and the methods of taking them out are different. If you don't understand, follow the method I explained before to perform binary conversion, storage and take out. In order to avoid the inertia of thinking, I will not explain this question again.

Guess you like

Origin blog.csdn.net/m0_74316391/article/details/130980796
Recommended