C++: Difference between const and define

1: const is a constant data type, which is stored in the data segment of the program, and define is only for text replacement, and exists in the code area of ​​the program;

2: Introverted function, no addressing process is required, the function is directly expanded, so it will not jump, it is faster, and the code is copied when compiling;

3: The default inheritance of struct is public, and the default inheritance of class is private;

4: typedef is mainly used to define new data types, and define is mainly used to define variables;

Variable storage location and code logic relationship
The variable address we usually ask for is a logical relationship here. The storage relationship in the real physical address is not the same. Even the same logical address may correspond to different physical addresses.

Initialized static variables and global variables are stored in the data segment close to the code segment. 
Uninitialized static variables and global variables are stored in the data segment close to the heap.

The location on the stack is the location of the environment variable , which is at the high address of the logical address. We usually generate executable files such as a.out or out, and its information is stored in the location of the uninitialized global variable (BSS). This location What is stored is all kinds of necessary information for executable code, which we will not discuss in detail here.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324667521&siteId=291194637