We all know that there are three ways to create objects in C + +, as follows:
#include <iostream>using namespacestd;classa{Private: intN; Public: A (intm): N (m) {}~A () {}};intMain () {A A (1);//Allocation in stacksA B = A (1);//Allocation in stacksA * c =NewA1);//Allocation in heapDelete C; return 0;}
There is no difference between the first and the second, an implicit invocation, an explicit invocation, both allocating memory in the stack in the process virtual address space, and the third using new, allocating memory in the heap, while the allocation and release of memory in the stack is managed by the system, and the allocation and release of the memory in the heap must be manually released by the programmer. So it's a question of whether to put objects on the stack or in the heap, which is related to the difference between the heap and the stack itself:
Here are a few questions:
1. Heap and stack the maximum amount of memory that can be allocated
2. Heap and stack memory management methods
3. Allocation efficiency of stacks and stacks
First of all, generally for a process stack size is much smaller than the size of the heap, in Linux, you can use Ulimit-s (in kilobytes) to see the maximum size of a process stack can be allocated, generally not more than 8M, some not even more than 2M, but this can be set, And for the heap you will find that the maximum assignable size for a process heap at the magnitude of G, different systems may not be the same, such as 32-bit system maximum not more than 2G, and 64 is the system maximum not more than 4G, so when you need a allocated size of memory, use new, that is, with the heap.
Second, for the second problem, the stack is the system data structure, for the process/thread is unique, its allocation and release by the operating system to maintain, do not need the developer to manage . When executing a function, the storage unit of the local variable within the function can be created on the stack, which is automatically freed at the end of the function execution. The stack memory allocation operation is built into the instruction set of the processor, which has a high efficiency, and the different operating systems have certain restrictions on the stack. Memory allocations on the heap, also known as dynamic memory allocations. The program uses malloc to request memory during the run, which is managed by the programmer itself, and is determined by the developer at what time it is allocated, how much is allocated, and when free is used to release the memory. This is the only memory that can be managed by the developer. The use of good or bad directly determines the performance and stability of the system.
From the above, but we need very little memory, and you can determine how much memory you need, please use the stack. Use the heap when you need to know how much memory you need at run time.
Finally, for the third problem, the stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack stack has a special instruction execution, which determines the efficiency of the stack is higher. The heap is provided by C + + function library, its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will follow a certain algorithm (the specific algorithm can refer to the data structure/operating system) in the heap memory to search for available enough space, if there is not enough space (possibly due to too much memory fragmentation), It is possible to invoke the system function to increase the memory space of the program data segment, so that there is a chance to divide the memory in sufficient size and then return. Obviously, the heap is much less efficient than the stack.
Add a few more points.
When you use a container, you will use the first method defined above directly.
Like what:
Std::vector<testbase> A;
So if a to save a lot of things, will not explode stack it.
In fact, although a is allocated on the stack of memory, but a of the stack space is very small.
sizeof (a) << Std::endl;
Output: 24
For example, vector, no matter how much push, the number of classes stored in the heap is new, and on the stack occupies only three of the size of the pointer.
Of course you can also new a container. This takes up the space of the stack with only one pointer size.
Three ways to create objects in C + + "Go"