[QandA C++] Summary of key knowledge such as sizeof, strlen, static, extern, typedef, const, #define, inline functions, etc.

Table of Contents

The difference between sizeof and strlen

static

extern “C”

typedef

const

#defineMacro definition

Comparison of constants defined by const and #define

inline function

The difference between inline functions and #define


The difference between sizeof and strlen

  1. sizeof is an operator and strlen is a library function.
  2. The parameters of sizeof can be data types or variables, while the parameters of strlenare only Strings ending with ‘\0’ can be used as parameters.
  3. The compiler calculates the result of sizeof at compile time, and the strlen function must be calculated at runtime >. And sizeof calculates the size of the data type in memory, while strlen calculates the actual length of the string (excluding \0)
  4. strlen When calculating, if the string does not end with ‘\0’, the search will continue until ‘ is encountered. \0’
char str[] = "hello";
sizeof(str) = 6; // hello(5) + \0(1) = 6
/* str is not a pointer, but a character array! */ 

static

static functions:

  1. Constitute hidden, (static functions, static variables are OK). When compiling multiple files at the same time, all functions without static prefix and Global variables have global visibility. Once functions and global variables are added static, they can only be visible in this file.
  2. Maintain the persistence of the variable content, and the original value can be obtained the next time it is used.
  3. static variables and global variables are stored instatic area (data segment), and will be during compilation Initialization is completed
  4. The default initialization is 0. Global variables also have this feature. The default value of all bytes in the static area in the memory is 0x00

Uses of static:

  1. Modify local variables. Local variables are placed on the stack, and their life cycle ends when the containing statement block ends. But if you use static After modifying a local variable, the variable will be stored in the static area, and its life cycle will continue until the end of the entire program execution. Butalthough the storage space and life cycle have changed, the variable’s The scope remains the sameand is still limited to its statement block.
  2. Modify global variables. For a global variable, it can be accessed not only by this file, but also by other source files in the same project Access it (just add the extern keyword). After modifying it with the static key, it changes its scope and becomes visible only in this file.
  3. Modify functions. Similar to modifying global variables, they both change the scope.
  4. Use in class:
  5. Modifying a function in a class means that the function belongs to a class and not to any object of this class. Static member functions cannot access non-static members of their own class.
  6. Modifying a variable in a class means that the variable is shared by all objects of this class, can be accessed without instantiating the object, and cannot be initialized inside the class. It is generally initialized outside the class, and initialized Do not add static.
  7. why declaration within class and initialization outside class
  8. The initialization of member variables is in the initialization list stage. They are static and do not belong to an object. They are only declared in the class, but must be initialized outside the class. Its life cycle is global and does not belong to an object. It belongs to all objects. , all classes
  9. Static member functions cannot be modified by virtual. Static members do not belong to any object or instance, so adding virtual has no practical meaning. Static member functions do not have this pointer, and the implementation of virtual functions is to assign them to each object A vptr pointer, and vptr is called through this pointer, so it cannot be virtual.
this pointer problem,
When a non-static member function of an object is called, the system assigns the starting address of the object to the this pointer of the member function.
    
Static member functions do not belong to any object.
So static member functions do not have this pointers,
Since it does not point to an object, it cannot access non-static members of an object

extern “C”

  1. extern is used before the declaration of a function/variable to indicate that this function/variable is defined elsewhere and should be referenced here.
  2. With #include, why do we need extern? Because extern can be declared in advance, it will speed up the compilation process of the program, which can save time
  3. In C++, the keyword for importC function is extern, extern “C” The main function isin order to correctly implement the C++ code to call other C language code. After adding it, the compiler will be instructed to compile this part of the code according to the syntax of the C language instead of C++.

typedef

  1. typedef is used todefine an alias for a data type to enhance the readability of the program sex.
  2. The execution time is different: typedef is part of the compilation process and has the function of type checking.
  3. The typedef definition is a statement, because a semicolon is added to the end of the sentence.

const

  1. To prevent a variable from being modified, you can use the const keyword. When defining a const variable, you need to initialize it, because there will be no chance to do it again in the future. Change it; const must be initialized when defined, const takes effect in the compilation phase.
  2. In a function, const can modify the formal parameter, indicating that the formal parameter cannot change its value inside the function.
  3. For member functions of a class, you can specify that the return value is of const type, which can limit the use of the return value and prevent it from being modified.
  4. In a class, if a member function is specified as const type, it means that it is a constant function. Constant functions cannot modify the member variables of the class
  5. Cont member functions can access other members, but non-const member functions cannot access const members.
  6. Cont objects can only call const-modified member functions (This function is called a constant function)

#define macro definition

  1. #define macro is expanded in the preprocessing phaseexpanded
  2. #define macro is just code expansion, replacing strings in multiple places without allocating memory
  3. The constants defined by #define have no type. There is no type check for macro definitions. Whether they are right or wrong, they are directly replaced< strong>, Therefore, constants defined by define are not conducive to type checking.
  4. And define is not a statement, so you must not add a semicolon at the end of the sentence.

Comparison of constants defined by const and #define

  1. Const constants have type checking, while #define-defined constants do not have type checking. The compiler can check whether the type of const constant is correct at compile time, while the constant defined by #define can only be checked at runtime.
  2. const constants are stored in the data section or code section of the program, while #define defined constants are stored in the preset in the processor’s text segment.
  3. The value of a const constant can be viewed during debugging, while the value of a #define-defined constant cannot be viewed during debugging.
  4. The scope of const constants is the entire program, while the constants defined by #define are only valid within the current file or function >.
  5. Const constants must be initialized when declared, while constants defined by #define can be initialized during program running.

To sum up, const constants are safer, more reliable, easier to debug and maintain than #define constants. Therefore, it is recommended to use const constants instead of #define constants when writing programs.

Inline function

  1. Inline functions are code inserted during compilation
  2. Inline functions are a mechanism used in C++ to improve the efficiency of function calls. An inline function is defined by adding the inline keyword before the function declaration, prompting the compiler to insert the function content directly at the call point instead of making a regular function call. This avoids the overhead of function calls.
  3. Inline functions are suitable for situations where the function body is small and is called frequently. For larger function bodies, inlining may cause code bloat, affecting the size of the executable file. Therefore, the compiler may choose toignore a request for an inline function and treat it as an ordinary function call. The final decision on whether to inline a function is made by the compiler.
  4. The definition of inline functions is usually placed in the header file to facilitate inline expansion in multiple source files. This is because the compiler needs to see the function’s definition at the call site in order to do inline expansion.
  5. Inline functions have no address
  6. Inline functions cannot have loop statements, recursive calls and other operations that may cause deadlock or stack overflow. In addition, inline functions cannot have global or static variables, because these variables require memory allocation at compile time, while inline functions require code embedding to be completed at compile time.
  7. Inline functions cannot be virtual functions,

Inline functions are expanded at compile time and have no runtime dynamic binding, while virtual functions require runtime dynamic binding. The nature and purpose of the two are different, so inline functions generally cannot be declared virtual.

Inline functions are directly expanded in the code to reduce the cost of function calls. Virtual functions are so that objects can accurately perform their own actions after inheritance. This is impossible to unify.

Inline functions are expanded at compile time, and virtual functions can be dynamically bound at runtime.

The value and significance of inline functions

  1. Reduce the overhead of function calls: The main purpose of inline functions is toreduce the overhead of function calls and improve the execution efficiency of the program.
  2. Inline functions are suitable for situations where the function body is small and is called frequently. When a function is called frequently, inline expansion can reduce the number of function calls, thereby improving program performance.
  3. Expanding smaller functions inline can make the code more compact and avoid layers of nested function calls. This helps improve code readability and understandability, and simplifies code maintenance.

The difference between inline functions and #define

  1. Macro definition is a simple text replacement mechanism that can replace specified identifiers with specified text before compilation.
  2. Inline functions are generally used for functions whose function body code is relatively simple and cannot contain complex control statements and recursion. It is used to improve efficiency and is used frequently in programs. and the little functions called are very useful.
  3. In the preprocessing stage of macro functions, all macro names are replaced with macro bodies, while inline functions are replaced during compilation During code insertion, the compiler will directly expand the content of the inline function wherever an inline function is called, thus eliminating the overhead of function calls and improving efficiency.
  4. Macro definitions are not type-checked. Whether they are right or wrong, they are directly replaced, while inline functions are not type-checked during compilation. Type checking will be performed. The preprocessor replaces function calls by copying macro codes, eliminating the need to push and pop functions onto the stack, improving efficiency.
syntaxbug.com © 2021 All Rights Reserved.