C language articles - data storage in memory

Learning points in this chapter

1. Detailed introduction of data types
2. Storage of shaping in memory: original code, inverse code, complement code
3. Introduction and judgment of big and small endian byte order


Text begins! ! !

Data types include

Key introduction: floating point type, enumeration type, pointer type, others will be introduced in detail later in chapters.

Floating point family:

Members: float, double

Note : Floating-point numbers cannot be directly judged equal by the equal sign, and ESP (precision) needs to be set.

For example, to determine whether a variable of type double is equal to 0, it cannot be directly a==0.

#include<stdio.h>
#include<math.h>
#define ESP 0.000000000000000001
int main()
{
	double a = 0.0000000001;
	if (fabs(a) < ESP)
	{
		printf("a等于0");
	}
	return 0;
}

 If the input decimal represents the double type of the literal value, if you need to input the float type decimal, you need to put f after the decimal.

 Workaround for the warning: float a = 0.2f.

enum type

definition

Enumeration type definitions are identified with the keyword enum in the form:

enum 标识符 
{
      枚举数据表
};

Notice:

(1) enum is a keyword that identifies the enumeration type. Defining an enumeration type must start with enum.

(2) The names in the enumeration data table are set by the programmer, and these names are just a symbol. But pay attention to the name to improve the readability of the program. An enumeration type is a collection, and the elements in the collection (enumeration members) are some named integer constants, and the elements are separated by commas.

(3) The values ​​of the enumeration data table are all integers. The default value of the first enumeration member is an integer of 0, and the value of subsequent enumeration members is increased by 1 to the previous member. You can also manually set the values ​​of enumeration members to customize the integers in a certain range.

(4) Enumeration constants can be initialized when the type is defined.

(5) Enumeration types can be compared.

(6) Enumeration constants are not strings, and strings cannot be output in %s mode.

(7) The enumeration type is an alternative to the preprocessing directive #define

pointer type

Here we mainly introduce the null pointer void*

Remember the following two points

1.void* can accept pointers of any type

2. Any type of pointer can accept void

In-memory storage of shapes

What we need to know is that the data is stored in memory as a complement!

We must first understand the following knowledge

The original code
can be directly translated into binary in the form of positive and negative numbers.
One's complement
The sign bit of the original code remains unchanged, and the other bits can be obtained by inverting the other bits in turn.
complement

Complement +1 to get the complement.

A number such as -1

Original code: 1000 0000 0000 0000 0000 0000 0000 0001

Complement: 1111 1111 1111 1111 1111 1111 1111 1110

Complement: 1111 1111 1111 1111 1111 1111 1111 1111

It is recommended to remember that -1's complement is 32 ones.
Why save the complement?

The cpu will only perform addition operations ( actually, complement addition ), and cannot perform subtraction operations, and the complement code can solve this problem very well

For example, 1-1 can be converted to 1+(-1)

1's complement is equal to the original code: 0000 0000 0000 0000 0000 0000 0000 0001

-1's complement is: 1111 1111 1111 1111 1111 1111 1111 1111
added together is 0000 0000 0000 0000 0000 0000 0000 0000

The result is 0, which is the magic of two's complement! !

Introduction to big and small

Big-endian (storage) mode means that the low-order bits of the data are stored in the high address of the memory, and the high-order bits of the data are stored in the low address of the memory.
Little-endian (storage) mode means that the low-order bits of the data are stored in the low address of the memory, and the high-order bits of the data are stored in the high address of the memory.

Low-endianness of data: in bytes, the lower the weight to the right.

The high-order byte order of the data: in bytes, the higher the weight is closer to the left.

Such as data int a = 0x11 22 33 44 

44 has the lowest weight, followed by 33. This is like the integer 1234, the weight of 1 is 10 to the third power, and the weight of 2 is 10 to the second power. 1 is the high full value bit and 4 is the low weight bit.

Tested with vs2019, the compiler places high bits at high addresses and low bits at low addresses, that is, vs2019 uses little-endian storage.

Next, we design a program that does not judge the data storage method (little endian or big endian) used in a certain environment by looking at the memory.

#include<stdio.h>
int check_sys()
{
	union
	{
		int i;
		char c;
	}un;
	un.i = 1;
	return un.c;
}
int main()
{
	int a = check_sys();
	if (a == 1)
	{
		printf("小端");
	}
	else
	{
		printf("大端");
	}
	return 0;
}

finally

If you have any questions about this chapter, please feel free to communicate with me.

If there is an error in this chapter, please point it out, I am very grateful.

If you find it useful, please like and comment, thank you.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324133103&siteId=291194637