C++ resolution of the difference between creating objects with and without new

  • 2020-04-02 01:22:46
  • OfStack

We all know that there are three ways to create objects in C++, as follows:


#include <iostream>
using namespace std;
class A
{
private:
    int n;
public:
    A(int m):n(m)
    {
    }
    ~A(){}
};
int main()
{
    A a(1);  //The stack allocation
    A b = A(1);  //The stack allocation
    A* c = new A(1);  //The heap allocation
  delete c;
    return 0;
}

The first and the second is no difference between a call, a explicitly call, both are in the process of the virtual address space allocated memory in the stack, and a third new, allocated memory in the heap, and the stack memory allocation and release is by the system management, and the heap memory allocation and release it must be manually by the programmer, so it is a question is put the object on the stack or heap of problem, the problem is the difference between the stack and heap itself and related to:

Here are a few problems:
1. Maximum allocatable memory size of heap and stack
2. Memory management of heap and stack
3. Heap and stack allocation efficiency

First according to the first question, generally for a process stack size is far less than the size of the heap, in Linux, you can use the ulimit -s (KB) to view a maximum allocation process stack size, generally no more than 8 m, some even less than 2 m, but this can set up, for you will find that, for a maximum allocation process heap size of the order of G, different system may be different, such as 32-bit system no more than 2 G, and 64 for the system is more than 4 G, so when you need a distribution of the size of the memory, Please use new, which is the heap.

Secondly, for the second problem, the stack is the system data structure, which is unique to the process/thread, and its allocation and release are maintained by the operating system, which does not need the developer to manage. When a function is executed, the locations of local variables within the function can be created on the stack and are automatically released when the function is executed. The stack memory allocation operation is built into the processor's instruction set, which is very efficient. Memory allocation on the heap, also known as dynamic memory allocation. The memory requested by the program during its run is malloc. This memory is managed by the programmer himself, and its lifetime is determined by the developer: when to allocate, how much to allocate, and when to free the memory. This is the only memory that can be managed by the developer. The quality of use directly determines the performance and stability of the system.

We know from the above, but we need very little memory, and you can determine how much memory you really need, please use the stack. When you don't know how much memory you need until runtime, use the heap.

Finally, for the third problem, the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the bottom: allocate special register to store the address of the stack, and push and push all have special instructions to execute, which determines the efficiency of the stack is relatively high. Heap is C/C + + library, its mechanism is very complex, for example in order to allocate a block of memory, library functions will be according to certain algorithm (specific algorithm can reference data structure/operating system) in the heap memory search available enough space, the size of size if there is not enough space (which may be due to too much memory fragments), it is possible to call system function to increase the memory space program data segment, so you have the opportunity to point to get enough memory size, and then to return. Obviously, the heap is much less efficient than the stack.

From above, we can use a stack if we can use a stack.


#include <stdio.h>
#include <stdlib.h>  
void main()
{
 int n,*p,i,j,m;
 printf(" This procedure can sort arbitrary integers ;n");
 printf(" Please enter the total number of integers : ");
 scanf("%d",&n);
 p=(int *)calloc(n,sizeof(int));    //The runtime determines the size of the memory allocation
 if(p==0)   {
  printf(" Allocation failure !n");  
  exit(1);  
 }


Related articles: