C++ asks keywords

Discrimination of const variables and static variables

static
After changing the local variable to a static variable, it changes its storage method, that is, changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use. Therefore, the static specifier plays different roles in different places.

Global variables and local variables are divided from the perspective of variable scope.
Static variables and dynamic variables are divided from the perspective of variable memory allocation.

Global variables themselves are static storage methods, and static global variables are of course static storage methods. There is no difference in the storage methods between the two. The difference is that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. of. The static global variable limits its scope, that is, it is only valid in the source file where the variable is defined, and cannot be used in other source files of the same source program.

Example 2: What is the difference between static global variables and ordinary global variables?

Answer: The storage area of ​​static global variables is the same as that of ordinary global variables, the difference is:

Static global variables are only valid in the file that declares this static global variable;

Ordinary global variables are valid for the entire source program, and when the source program contains more than one file, they are still valid for other files.

Example 3: What is the difference between static local variables and ordinary local variables?

Answer: The storage area of ​​static local variables is a static storage area, and the storage area of ​​ordinary local variables is a stack;

The life cycle of a static local variable is the entire source program, but it can only be called in the function that declares it, and its value is related to the last result; while the life cycle of an ordinary local variable is the life cycle of its function, beyond a specific range. The value will be reinitialized;

If a static local variable is not initialized, its value defaults to 0, while an ordinary local variable is undefined.

Discrimination of const variable
static variable

Variables stored in the static data area will be initialized when the program starts running, and it is the only initialization. There are two kinds of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. After all, static is still used to hide.

—Based on the above two points, a conclusion can be drawn: changing a local variable to a static variable changes its storage method, that is, changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use. Therefore, the static specifier plays different roles in different places

const

限定变量为只读常量,不可修改

insert image description here

const modified function parameters

(1) The passed parameters and the pointer itself are immutable in the function, meaningless!

void func(const int var); // 传递过来的参数不可变
void func(int *const var); // 指针本身不可变

Indicates that the parameter cannot be modified in the function body, but it has no meaning here, var itself is a formal parameter and will not change in the function. The same goes for including the incoming formal parameter as a pointer.
The input parameter adopts "pass by value". Since the function will automatically generate a temporary variable for copying the parameter, the input parameter does not need to be protected, so do not add const modification.

(2) The content pointed by the parameter pointer is constant and immutable

void StringCopy(char *dst, const char *src);

where src is an input parameter and dst is an output parameter. After adding const modification to src, if the statement in the function body tries to change the content of src, the compiler will point out an error. This is one of the functions of adding const.
(3) The parameter is a reference, in order to increase efficiency and prevent modification.

void func(const A &a)

static

insert image description here

volatile

insert image description here
Tell the compiler that the value of the variable is unstable and may be changed. \nNeed to read the value in the memory instead of reading the backup in the register. A
variable used by multiple threads, and the value of the variable will be Changed
Non-automatic variables accessed in the interrupt service subroutine Variable characteristics
of parallel device hardware register variables (such as status registers) Variable Non -optimizable Tell the compiler not to perform various optimizations on variables declared by volatile\nProgrammers are guaranteed to write in The instructions in the code must be executed volatile int a;\na = 1; if not declared as volatile\n the two codes will be merged into one. Sequential execution (atomicity) guarantees the order between volatile variables and will not be compiled Out-of-order optimization can be used with const Yes , const is read-only, volatile is to read from the memory The pointer can be volatile to modify function parameters











inline

insert image description here

inline is just a suggestion now. The function defined in the class requests the inline inline
function by default to modify any function. This keyword is a suggestion. It tells the compiler that this function should be implemented as an inline function. State-of-the-art modern compilers are optimizing compilers that decide for themselves which objects to inline, so a keyword that was once considered important is now just a suggestion to the compiler.
The compiler will directly expand the content of the inline function at every place where the inline function is called, which can save the cost of calling the function and improve efficiency. It is generally used for functions with relatively simple code in the function body and cannot contain complex
controls statements, while, switch, and inline functions themselves cannot directly call themselves. If the function body of the inline function is too large, the compiler will automatically turn the inline function into an ordinary function.
When an ordinary function is called, the system first executes the function body at the entry address of the function, and returns Execution continues at the place where the function is called, and the function always has only one copy. \n The inline function does not need addressing. When the inline function is executed, the function will be expanded. If the inline function is called N times in the program, there will be N times to expand the function code. Features: use space for time
, Improve the efficiency of function calls.
The difference between inline and macro definition

	宏定义的替换在预处理时期,而内联函数在编译时期
	宏定义没有类型检查,只是文本替换;内联函数有类型检查,是真正的函数
	宏定义和内联函数使用的时候都是进行代码展开,对于短小的函数,都能提高效率

typedef

给已存在的类型定义一个别名
typedef由编译器解释,不是预处理器
在其受限范围内,typedef比#define更灵活。
与#define不同,typedef创建的符号名之受限于类型,不能用于值。

insert image description here

extern

使用其他源文件的全局变量和函数,extern int a;

extern "C"
	C是表示编译连接规约
	实现C和C++混合编程

insert image description here

typedef

给已存在的类型定义一个别名
typedef由编译器解释,不是预处理器
在其受限范围内,typedef比#define更灵活。
与#define不同,typedef创建的符号名之受限于类型,不能用于值。

sizeof sizeof is an operation symbol??

以字节为单位返回所指对象所占的内存空间

insert image description here

The difference between sizeof and strlen()

insert image description here

wild and dangling pointers

The pointer should be initialized when it is defined, you can use nullptr (c++) NUL
(c)

insert image description here

struct structure union is more from the perspective of C language

insert image description here

struct structure memory alignment rules:

struct S2
{
    
    
    char c1;
    int i;
    char c2;
};
printf("%d\n", sizeof(struct S2));

The result is 12.
The first member is at the address at offset 0 of the structure variable.
Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number). Alignment = The smaller value between the compiler's default alignment and the member size. The default value in vs is 8, and the default value in Linux is 4.
The total size of the structure is an integer multiple of the maximum alignment number. (Each member variable has its own alignment number)
If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment number, and the overall size of the structure is all maximum alignment numbers (including nested structures Integer multiples of body alignment).

Why does memory alignment exist?
Platform reasons (porting reasons):
Not all hardware platforms can access arbitrary data at any address; some platforms can only fetch certain types of data at certain addresses

Performance reasons:
Data structures, especially stacks, should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned memory, the processor needs to do two accesses to memory; while an aligned memory access requires only one access.
Disadvantages:
There is nothing wrong with it: there will inevitably be efficiency problems. This is an approach of exchanging space for time, but this approach is worthwhile

class和struct

insert image description here

区别
	默认访问权限和继承关系不同,class是private,struct是public
	class可以定义模板类形参,比如template <class T, int i>
使用
	类比结构体增加了操作数据的行为,就是函数
	struct用于数据结构集合
	class用于对象的继承多态封装,虽然struct也可以
如何计算结构体长度
	内存对齐问题

おすすめ

転載: blog.csdn.net/qq_46084757/article/details/127002284