The difference between using new in C + + to open up memory space and non-new memory space

Source: Internet
Author: User

#include <iostream>using namespace Std;class a{private:int n;public:a (int m): N (m) {} ~a () {}};int m  Ain () {A A (1);  Allocate a B = a (1) in the stack;  Allocate A * c = new A (1) in the stack;    Allocation of delete C in the heap; return 0;} Copy code The first and the second is no different, an implicit invocation, an explicit invocation, both are allocated memory in the stack in the process virtual address space, and the third uses new, allocates memory in the heap, and the allocation and release of memory in the stack is managed by the system. The allocation and release of memory in the heap must be manually released by the programmer, so this creates a problem whether to put the object on the stack or in the heap.  This problem is related to the heap and the stack itself: there are several problems: 1. Heap and stack maximum allocated memory size 2. Heap and Stack memory management Method 3. Heap and stack allocation efficiency first, in general, the size of a process stack is much smaller than the size of the heap, in Linux, you can use Ulimit -S (in kilobytes) to see the maximum assignable size of a process stack, generally no more than 8M, some not even more than 2M, but this can be set, and for the heap you will find that the maximum size of a process heap can be allocated to the magnitude of G, different systems may not be the same, For example, the 32-bit system is 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,Calculate the opportunity to support the stack at the bottom: the allocation of dedicated register storage stack address, the stack stack out of a special command execution, which determines the efficiency of the stack is high. 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. From the above, you can use stacks. Copy code # include <stdio.h> #include <stdlib.h> void Main () {int n,*p,i,j,m; printf ("This program can sort any integer; \ n"); printf (" Please enter the total number of integers: "); scanf ("%d", &n);    p= (int *) calloc (n,sizeof (int));    The runtime determines the memory allocation size if (p==0) {printf ("Allocation failed!\n");   Exit (1); }

The difference between using new in C + + to open up memory space and non-new memory space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.