C++ learning record essay

They are all essays, many of which are essays from the third edition of "High Quality Programming Guide to C++".

C++ compilation steps:

hello.c -> precompiled -> hello.i -> compiled -> hello.s -> assembled -> hello.o -> linked -> executable file

  1. Precompilation will process the precompilation directive (usually starting with #, and only whitespace characters can appear in front of it) to generate an intermediate file as input to the compiler.

    • #include (All contents of the header file will eventually be merged into one or several source files. The source file formed after recursively expanding all header files is called a compilation unit)
    • #define (usually replaced with corresponding text) #define is just a simple replacement without grammar checking. (Leave the compiler to check)
    • #ifdef
    • #pragma
    • String construction operator (# ##), #(variable generates string), ##(string generates variable)
    • __LINE__ __FILE__wait
    • Delete all comments
  2. Compile the smallest compilation unit separately (all files included recursively in a single header file)

  • Gramma analysis:
  • Semantic Analysis:
  • Intermediate code generation
  1. Assembly: to be written
  2. Linking: Integrate each compilation unit and link the dynamic library
  • The user program calls the library (library composed of header files and binary libraries) interface. The connector will extract the corresponding code from the library and connect it with the user program to generate an executable file or dynamic link library file.

Runtime: pending writing

OC compilation steps:

OC -> 中间代码(.ll) -> 汇编、机器代码
The .ll file is common to all platforms.
Transform command: clang -emit-llvm -S main.m

Long expression splitting needs to be split into multiple lines at low precedence, and the operator is placed at the beginning of a new line (to show protrusion)

  if (aaaaaaa > bbbbbbbbbbbb)
  && (ccccccccc > ddddddddd)
  && (eeeeeeeee > fffffffff)
{
    
    
  pass
}

Recommended 以行为为中心 layout (ADT/UDT) is to write public members in front

The layout of 以数据为中心 is not recommended, so write private in front of it

The most fundamental change of c++ to c is to put functions into structures, thus creating C++ classes

Dynamic characteristics VS static characteristics

  • Static characteristics: The program can be determined at compile time.
  • Dynamic properties: Not static properties
  • Dynamic features are one of the most powerful features of object-oriented languages ​​because they support scalability, one of the most important goals of a program.

Dynamic characteristics:

  • C++: C++ virtual functions (polymorphic methods that determine which base class to call), abstract base classes (base classes for pure virtual functions), dynamic binding, runtime polymorphism

    • Abstract base class: It is mainly used for separation of interface and implementation. It is a complete encapsulation, creates an instance of a subclass, and uses the object address of the base class to expose external access. (Only header files and binary files are released, kept confidential) (tip: The destruction of the base class must be virtual destruction, otherwise the subclass cannot be destructed)
    • Base class: The destructor must be a virtual function, otherwise if the subclass is converted to the base class, the destructor will not enter the destructor of the subclass.
    • Polymorphic class: Every class with virtual functions is called a polymorphic class (virtual functions are added by oneself or inherited). See specifically# 虚函数表Vtable和 Vptr 一节

    Do not use arrays to directly store polymorphic objects, but store base class pointers or base class smart pointers. Because storing direct polymorphic objects will cause the +sizeof (base class) memory to be searched for each array subscript, causing the second and subsequent addresses to be misaligned.

  • OC: Dynamic type (id), polymorphic binding ([obj msgSend]), polymorphic loading (picture 2x3x replacement, dynamic addition of methods and variables )

Virtual function table Vtable and Vptr

  • Every polymorphic class has one or more vtables and vptrs.
  • Each polymorphic class has a virtual function table (vtable), which stores all the virtual function addresses of the class and information about the class.
  • The implicit pointer member of vptr (pointing to the virtual function table of the current class).
  • The position of the corresponding virtual function in the derived class vtable is the same as that of the base class. The currently newly added virtual function is added to the end of the vtable. (This maintains compatibility with vtable layouts of derived classes)
  • vtable and vptr must be created implicitly at the very beginning of the init method with vptr pointing to the vtable.
  • Pseudo code for base class calling virtual function: (*(p->_vptr[slotNum]))(p, arg-list); p: base class pointer, vptr: refers to p The implicit pointer of the object pointed to, slotNum: The number of the virtual function called in the vtable (determined at compile time)

Insert image description here

Memory image distribution of object class

  • Member variables: User memory area (including OC's isa, C++'s _vptr)
  • Method (function): including static function, code segment (to be written to determine whether it is)
  • Other static variables: Program static data area
    Therefore, the object itself only consists of data, and any member function does not belong to any object. The relationship between non-static member functions and objects It is binding, and the intermediary of binding is this pointer

struct sizeof

When storing variables, addresses must be aligned. The compiler will follow two principles when compiling a program:

  • Rule 1: The offset of a member in a structure variable must be an integer multiple of the member size (0 is considered an integer multiple of any number)
  • Rule 2: The size of the structure must be an integer multiple of the size of all members, that is, a common multiple of the size of all members.
struct stru1
{
    
    
  char b;  //start address is 0
  short s;  //start address is 2 注意这里是2 不是1(根据规则一)
  int a;  //start address is 0
};

Constructor member initialization list

  • We are generally used to initializing member data within the constructor function. This is not real initialization, but assignment, although it is generally regarded as initialization.
  • The real member initialization is in the "initialization expression table", that is, before {} in the constructor. The initialization work in this description list occurs before any code in the function body is executed, and the compiler does this.
  • The order of the initialization list is not the written initialization order. Instead, the compiler writes the initialization list in the order of declaration in the class. So it is best to adjust the order
    1. Call the base class constructor
    2. Initialize the members of this class (preferably in dependency order)
    3. Other members of the function body.

object construction order

  • It's very simple without a base class, just initialize it directly.
  • If it is a derived class object.
    Construction process. It is a layer-by-layer construction from base class to subclass. It is also understandable. After all, the section 虚函数表Vtable和 Vptr concluded that vtable is always created first by the base class, and then the subclass copies and then adds its own 新虚函数
最基类的构造函数
最基类成员对象的构造函数
.......其他基类
次基类的构造函数
次基类成员对象的构造函数
自己的构造函数
自己的成员对象的构造函数
  • Destruction is a call from the subclass to the base class layer by layer. (The order of destruction is strictly in accordance with the order in which construction is reversed)
  • virtualThe function keyword tells the compiler that the same member functions in the derived class should be placed in vtable and replace the corresponding member functions of the base class;

Default constructor does not create

  • If a default constructor is not explicitly defined but a single-argument constructor is defined, the latter will prevent the compiler from generating the former. So the class does not have a default constructor. At this time, an error will be reported when defining the object.
struct  T
{
    
    
	T(int){
    
    }; //显式创建的构造函数导致 默认构造函数不存在
	string p;
};

Timing of calling constructors and destructors

Global objects are initialized before main, but the order is undefined. It is destructed after main() is finished.

Insert image description here
Insert image description here

Copy construction and assignment construction

  • Very easy to confuse.
  • Copy construction: Use another existing object to initialize during initialization (When initializing)
  • Assignment construction: The object can only be assigned to an existing object (Already created)
String c= a; //调用拷贝构造,但是风格不好,应该使用 String c(a)
c = a; //赋值

Extern C

void __cdecl foo(int x, int y);

By default, the c++ compiler will generate an internal name like this:__foo_int_int to support function overloading.
But the internal name generated by the C compiler is: _foo., because it cannot be overloaded.
Therefore, an explicit declaration extern C is required to tell the C++ compiler that this is a C link function and instruct the linker to find the function definition in the C library.

Function overloading, overriding and hiding

Overload

  • Have the same scope (i.e. in the same class)
  • The function names are the same. Parameter type, order, and number are not passed (including const and 非const)
  • virtualDispensable.

cover (override)

  • different scopes. Base and derived classes
  • Same name
  • Parameters consistent
  • Base class functions must be virtual functions

hide

Indicates that a derived class member function shadows a base class member function with the same name.

  • The derived class has the same name as the base class function, but the parameter list is different< a i=4>. Whether or not is present, base class functionsare hidden. (not overloading)virtual
  • The derived class has the same name as the base class, and the parameter list is alsothe same, but the base class has no keyword, and the base class function will be hidden. (not an override)virtual

++/ – and its overloads

use

For example: ++a, a++
Note: ++ Pre-version means executing it first< a i=4>, and then take the value. The post-fixed version indicates that the value calculation is performed first, and then is performed. +1+++1

Overload

C++ standard. When overloading the operator for ++ / --.

  • Overloaded ++a, no parameters are required. Integer & operator++(){} Pre-release version
  • Overloadinga++ requires an int parameter as a flag (that is, a dummy parameter, a non-named parameter). Integer operator++(int){} Post-version.

Replace macros with inline functionsinline

Comparison of inline and macro

  • The inlining design of C++ was originally designed to improve the efficiency of functions.
  • Macros can be used in C to improve efficiency. But the macro code is not a function, it just acts like a function.
  • Compilation preprocessing replaces function calls by copying macro codes, thus eliminating the need forparameter pushing on the stack and generating CALL calls in assembly language , return parameters, and execute the return process, thus improving efficiency.
  • The disadvantage of macros is that they are prone to errors, such as#define MAX(a, b) a> b ? a : b
  • Macros cannot be debugged, but inline functions can be debugged. Because the debug pattern is not expanded and can be called like an ordinary function, release is actually inlined.
  • Inline can be debugged. Inline functions without errors, declarations, names, types, return value types and function locales will be put in In the symbol table, when calling an inline function, the compiler will first check if there are any errors. If not, it will directly Replacecall statement.
  • inineThe keyword must be placed together with the implementation statement and is the keyword for implementation.

Inline cannot be abused

The following are not suitable for use:

  • The code of the function body is relatively long, which will make the executable code too large.
  • If the code in the function body loops or the execution time is too long, then the execution time is much longer than the pop-and-stack time saved by the call.

The difference between memmove and memcpy

  • memmove and directly replace the original content one by one, the content may be overwritten.
  • memcpy will not cause content overwriting

container

std::Vector (vector) and linked list (linked list)

corresponds to the most basic containers of STL: dynamic array and linked list structure
a> also represents the two basic methods of memory storage: continuous storage and random Storage(discontinuous storage)
Different storage methods determine different access methods of elements: Random accessSequential access: can only be accessed from the first element: An access method to obtain the memory address of any element with constant overheadRandom access. sequential access and

  • vectorSupports random access and sequential access
  • listSupports sequential access

Tip

  • Associative container -> Element searchRelatively fast
  • Sequential container -> Insertion and deletionOperations are faster
    Insert image description here

iterator iterator

The valid element range of the STL container, in which the last of the iterator either points to the end of the last valid element or points to a blank node, anyway, it does not point to the last valid element. The principle of front closing and back opening is followed, that is, [first, last )
Insert image description here

Capacity and actual size

The items between start and finish are valid elements, and the items between start and end_of_storage are the total capacity. The controls that are not used later are redundant capacity and do not belong to the container.
The excess capacity is not initialized and is only reserved for subsequent elements.
You can view the capacity and element control size through capacity() size().

  • void reserve(size_type n) is the size reserved for container requestscapacity. When the actual size of the existing container is greater than the allocated amount, will have copy constructors, etc., otherwise they cannot be placed in the container)). (so basic types such as Copy constructor to the new position (calling from the old position, and copy all elements to a larger continuous control whose size is the number of existing elementsvector will be reallocated in the free memory arean * sizeof(T)int
  • Therefore, you should try to add elements to it after exceeding the capacity as frequently as possible, because moving and copying will occur frequently.
    Insert image description here

STL container summary

Insert image description here
Insert image description here
Insert image description here

sizeof VS strlen

char s1[] = "";
sizeof(s1) // 1
strlen(s1) // 0

sizeof() will add the length of '\0'
strlen() will stop when encountering '\0' and will not add '\0'

Outer flat stack VS inner flat stack

http://mallocfree.com/basic/c/c-6-function.htm#79

  • __cdecl
  • __stdcall
  • __fastcall

Guess you like

Origin blog.csdn.net/goldWave01/article/details/121083259