Cause:
Recently, we have integrated C ++ third-party class libraries in objective-C code, so we have come into use the C ++ initialization object method. The following is a brief introduction.
Two Methods for initialization:
Method 1: classname object (initialization parameter );
Method 2: classname object = new classname ();
Differences between the two initialization methods:
The first method of initialization: Allocate space in the stack and release it automatically.
Method 2: Allocate space in the heap and manually release it.
Introduction to objects in different memory regions:
In C ++, objects are usually stored in three memory areas: Stack, stack, and global/static data areas. Correspondingly, objects in these three regions are called stack objects, heap objects, and global/static objects.
Global/static data zone: Global Objects and static objects are stored in this zone. objects in this memory zone are released only after they are created until the process ends. It can be accessed by multiple threads during its lifetime. It can be used as a method for multi-thread communication. Therefore, thread security should be considered for global and static objects, especially for local static variables in functions, it is easy to forget its thread security. Global Objects and some static objects have one feature: the initialization operation of these objects is prior to the execution of the main function, and the initialization sequence of these objects (which may be distributed in different source files) is not specified, therefore, do not start threads during their initialization, and their initialization operations should not have dependencies.
Heap: the heap object is an object that dynamically allocates memory in the heap through new/malloc and releases memory through delete/free. We can precisely control the creation and destruction of such objects. Heap objects are widely used in C ++. Heap objects can be used for object sharing between different threads and functions. Heap objects are also used for large objects (stack space is limited ), in particular, virtual function polymorphism is generally implemented by heap objects. Using Heap objects also has some disadvantages: 1. the programmer needs to manage the Life Cycle. If he forgets to release the program, there will be Memory leakage. Multiple releases may cause program crashes. This problem can be avoided through smart pointers. 2. the time efficiency and space efficiency of heap objects are not as high as those of stack objects. Heap objects generally use a certain search algorithm to find the appropriate memory size in the heap, which is time-consuming, in addition, the size of memory allocated from the heap is several bytes larger than the actually applied memory, which is space-consuming, especially for small objects. 3. frequent use of new/delete heap objects can cause a large amount of memory fragments and insufficient memory usage. Two or three problems can be solved through memory allocation once and multiple usage. A better way is to implement a specific memory pool based on business characteristics.
STACK: stack objects are self-generated and self-destroyed objects. programmers do not need to manage their lifecycles. Generally, temporary objects and local objects in functions are stack objects. Stack objects are efficient because they do not need to perform memory searches and only move the top pointer of the stack. In addition, stack objects are thread-safe because different threads have their own stack memory. Of course, the stack space is limited, so it is necessary to prevent Stack Overflow during use. Usually large objects, large arrays, and recursive functions must use stack objects with caution.