2020 bytes beating side by side

1. Vector expansion mechanism

In C ++, the elements of vector are stored in memory sequentially. When the space requested by the vector is occupied by the elements, you need to apply for a new empty larger memory. If you apply for memory by increasing the size of an element each time , CPU overhead is relatively large. In order to solve this problem, the size of the new memory application in C ++ is much larger than the original, instead of just one element larger, copy the elements from the old address to the new address. Generally speaking, the vector in gcc is expanded twice. After the expansion, because the vector moved to the new memory space, the original iterator will be invalid. In addition, inserting, adding, and copying will cause the expansion of the vector.

2. Why not initialize the variable in the constructor

When the class object is a global variable, the initial value is 0 when the system loads. When the class object is a local variable, space is allocated on the stack, and the value of the member variable is undefined. When the function is called, the stack will allocate a part of the space to store the local variables (including parameters) in the function, what is the original content in this newly allocated storage space, what is the initial content of the local variable, so the initial local variable The value is unpredictable.

3. Initialization of constructors and objects

The constructor is not to allocate memory space to the object, but to initialize the allocated memory space.

4. C ++ memory space allocation

C ++ memory is divided into 5 areas, heap, stack, free storage area, global storage area, and constant storage area.

Stack: When the function is executed, the storage units of local variables in the function can be created on the stack, and these storage units are automatically released when the function execution ends. Stack memory allocation operation is built into the processor's instruction set, which is very efficient, but the allocated memory capacity is limited.

Heap: These are the memory blocks allocated by new, their release compiler does not care, and is controlled by our application, generally a new corresponds to a delete. If the programmer does not release it, the operating system will automatically recycle it after the program ends.

Code storage area: store code

Global storage area: stores global variables and static variables, will be initialized.

Constant storage area: stores constants and cannot be modified.

The difference between heap and stack:

stack Stack
Management method Compiler management Programmer Manual
size of space Larger space Less space
Fragmentation problem new / delete will cause space discontinuity Come out first, no debris
Growth direction Upward growth Downward growth
Allocation Dynamic allocation Static allocation + dynamic allocation
Distribution efficiency low efficiency efficient
5. Object memory mechanism
  1. Statically create an object : If you declare an object directly with A a, the compiler allocates memory for the object in the stack space by directly moving the top pointer of the stack, moving out the appropriate space, and then (compiler) on this memory space Call the constructor to form a stack object. Using this method, call the class constructor directly. When finished using, the compiler automatically calls the destructor to release the stack memory.
  2. Create objects dynamically : Use the new operator to create objects in the heap space. This process is divided into two steps. The first step is (the compiler) executes the operator new () function, searches for suitable memory in the heap space and allocates it; the second step is to call the constructor to construct the object and initialize the memory space. This method indirectly calls the class constructor. When the object is used, delete will also call the destructor to release the memory on the heap
  3. Non-static class member variables: placed in the object's memory space
  4. Static class member variables: placed in the global constant storage area
  5. Ordinary member function: It is regarded as a global function of class scope and stored in the code area.
  6. Virtual function: stores the virtual function pointer (4 bytes) in the memory space of the object.
  7. Virtual function table: The virtual function table can be determined at compile time, so it is stored in the static storage area, similar to the static member in the class.
6. Calculation of class size:
  1. Empty class: The sizeof of the empty class is 1, and the compiler will add one byte to the empty class, so that after instantiation, the class can have a unique address.
  2. Classes containing only virtual functions : The virtual function pointer size is 4 bytes, so the size of the object is 4 bytes.
  3. Virtual inheritance: The size of the virtual base class pointer is 4 bytes.
  4. Classes containing data members : byte alignment
7. The difference between new and malloc
new malloc
Return type Object type pointer void *, forced conversion
Allocation failed bac_alloc exception NULL
parameter No need to specify size Specified size
Memory area Free storage area stack
Memory expansion Unable to expand Use realloc
Overload Can be overloaded Does not support overloading
8. C ++ file running process (static link and dynamic link)

Static linking: Link related files to form a static library, and connect with the target file as a unit.

Dynamic linking: The basic idea of ​​dynamic linking is to split the program into relatively independent parts according to modules, and link them together to form a complete program when the program is running, instead of linking all program modules into static links A separate executable file. Space saving, but linking is very slow at runtime.

Guess you like

Origin www.cnblogs.com/hang-shao/p/12734782.html