About the storage of data in memory (shaping)

Table of contents

1. Introduction to data types

 1. Plastic surgery family

2. Floating point family

3. Constructed types (custom types - we can create new types ourselves)

1.Array type

2. Structure type struct

3. Enumeration type enum

4. Union type union

4. Empty type

5. Pointer type

2. Storage of shaping in memory

1.Weight

2. Original code, complement code (binary)

3.Introduction to big and small endian

Big-endian storage:

 Little-endian storage:


1. Introduction to data types

 1. Plastic surgery family

type Number of bytes Ranges
int (shape) 4

-2147483648~2147483647

short(short integer) 2 -32768~32767
long (long integer) 4/8 -2^31~(2^31-1)
long long (double long type) 8 -2^63~(2^63-1)
char 1 -2^7~(2^7-1)

The int family is divided into unsigned and signed. Int is actually a signed int, (signed) int. For signed ones, we generally omit signed.


The above tables are actually all signed types.

        

type Number of bytes Ranges
unsigned int (unsigned integer) 4 0~(2^32-1)
unsigned short(unsigned short integer) 2 0~(2^16-1)
unsigned long(unsigned long integer) 4/8 0~(2^32-1)
unsigned long long (unsigned long integer) 8 0~(2^64-1)
unsigned char (unsigned character type) 1 0~255

The difference between signed types and unsigned types is that the unsigned type puts all the memory for negative numbers into positive numbers.​ 

sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

#sizeof is an operator that measures the length of a type or variable. %zu is used to specifically print the size_t type value, which is the return value of sizeof.


char represents a character, but the essence of the char type is an ASCII code value, which is essentially an integer, so it is classified into the integer family.

  •  The ASCII code for the capital letter 'A' is the decimal number 65, and the range of capital letters is 65~90.
  • The ASCII code of the lowercase letter 'a' is the decimal number 97, and the range of lowercase letters is 97~122.
  • Lowercase decimal number = Uppercase decimal number + 32

2. Floating point family

Floating point types can be used as long as decimals are used.

type Number of bytes effective number Numeric range (absolute value)
float 4 6 0 and 1.2*10^(-38)~3.4*10^38
double 8 15

0 and 2.3*10^(-308)~1.7*10^308

long double 8

15

0 and 2.3*10^(-308)~1.7*10^308
long double 16 19 0 and 3.4*10^(-4932)~1.1*10^4932
  • Different compilation systems handle long double types differently, and the bytes allocated will also be different.

significance:

  • Use this type of space to open up space (the size determines the scope of use)
  • How to look at the memory space perspective

3. Constructed types (custom types - we can create new types ourselves)

1.Array type

int arr1[5];   类型: int [5]
int arr2[8];         int [8]
char arr2[5];        char [5]

2. Structure type struct

                      Structure introduction   Structure introduction

3. Enumeration type enum

4. Union type union

4. Empty type

void test(void)
{
	printf("hehe\n");
}

int main()
{
	test();
	return 0;
}

The first void means that the function will not return a value

The second void indicates that the function does not need to pass any parameters

5. Pointer type

int* p;
char* i;

2. Storage of shaping in memory

1.Weight

Numerical values ​​have different representations: binary, octal, decimal, and hexadecimal.

Report decimal in different bases 21

Binary:0b10101 (0b means the representation is binary)

Octal:025 (0 before 25 indicates that the representation is octal)

Hex:0x15 (0x means the representation is hexadecimal)

The computer's numerical storage method is binary, and the capacity of int is 4 bytes, which is 32 (2^8) bits.

Each position is represented by 1 or 0. In computers, this is:

00000000000000000000000000010101 (32 digits in total)

We can understand what weight is by analogy with the decimal system. The decimal number 123 can be regarded as 1*10^2+2*10+3*1=123

And 10^2 is the weight of 1, 10 is the weight of 2, and 1 is the weight of 3.

2. Original code, complement code (binary)

 Computers use complement codes when manipulating numbers.

The original code, complement code and complement code of positive numbers are all the same.

int a = 21;
	//00000000000000000000000000010101-原码
	//00000000000000000000000000010101-反码
	//00000000000000000000000000010101-补码

 The original code, complement code and complement code conversion rules of negative numbers:

Original code: The binary sequence written directly in the form of positive and negative is the original code

One's complement code: The sign bit of the original code remains unchanged, and the other bits are inverted bitwise to obtain the one's complement code.

Complement code: the complement code + 1 is the complement code

int b = -21;
	//10000000000000000000000000010101-原码
	//11111111111111111111111111101010-反码
	//11111111111111111111111111101011-补码

 What you can see is that the signs of positive numbers and negative numbers are distinguished by the first bit.

analogy:

The number expressed by 100000000000000000000000000000001 is -1

000000000000000000000000000000001 represents the number 1

3.Introduction to big and small endian

The way data is stored in memory is divided into big-endian byte order and little-endian byte order.

Different compilers use big endian or little endian

Big-endian storage:

  • Storing the high-endian content of a data at a low address and placing the low-endian content at a high address is big-endian.

int a =0x11 22 33 44 (hexadecimal is used here for expression)

 Little-endian storage:

  • Storing the low-endian content of a data at a low address and placing the high-endian content at a high address is little-endian.

Of course I will verify it for everyone:

 I am using mcvs, and you can see that it is stored in little-endian byte order.

The 15 00 00 00 seen on the memory interface is hexadecimal for the convenience of our observation. In fact, what is counted in the computer is binary.

Guess you like

Origin blog.csdn.net/2302_79491024/article/details/134032996