C + + Create objects with new and do not create objects with the difference resolution _c language

Source: Internet
Author: User

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

Copy Code code as follows:

#include <iostream>
using namespace Std;

Class A
{
Private
int n;
Public
A (int m): N (m)
{
}
~a () {}
};

int main ()
{
A A (1); Stack allocation
A B = a (1); Stack allocation
A * c = new A (1); Allocations in the heap
Delete C;
return 0;
}




the first and second are no different, an implicit invocation, an explicit invocation, both of which allocate memory in a stack in the process virtual address space, while the third uses new to allocate memory in the heap, while 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 question of whether the object is placed on the stack or in the heap, and the problem is related to the difference between the heap and the stack itself:

Here are a few questions:
1. Heap and stack maximum allocated memory size
2. Heap and stack memory management mode
3. Heap and stack allocation efficiency

First of all, for the first problem, generally 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 view the maximum size of a process stack, generally not more than 8M, some even not more than 2M, but this can be set, And for the heap, you will find that the maximum allocated size for a process heap is at the order of magnitude G, different systems may not be the same, such as the 32-bit system Max no more than 2G, and 64 is the system maximum is not more than 4G, so when you need a allocated size of memory, please use new, that is, with the heap.

Second, for the second problem, the stack is a system data structure, for the process/thread is unique, its allocation and release by the operating system to maintain, do not need developers to manage. When the function is executed, the storage units of the local variables within the function can be created on the stack, and the storage units are automatically released when the function finishes. Stack memory allocation operations are placed in the processor's instruction set, the efficiency is very high, different operating systems on the stack have a certain limit. Memory allocations on the heap, also known as dynamic memory allocations. The program uses the memory requested by the malloc during its run time, which is managed by the programmer, whose lifetime is determined by the developer: when to allocate, how much to allocate, and when free 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, you can determine exactly how much memory you need, please use the stack. And when you need to know how much memory you really need at runtime, use a heap.

Finally, for the third problem, the stack is the machine system provides the data structure, the computer will be at the bottom of the stack to provide support: the allocation of special registers to store the stack address, pressure stack out of the stack have specific instructions to execute, which determines the stack of high efficiency. The heap is provided by the C + + function library, and its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will search the heap memory for the available space of sufficient size in a certain algorithm (the specific algorithm can refer to the data structure/operating system), if there is not enough space (possibly due to too much memory fragmentation), It is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to get enough memory and then return. Obviously, the heap is much less efficient than the stack.

From the above, can be used stack with the stack.

Copy Code code as follows:

#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)); Run time determines memory allocation size
if (p==0) {
printf ("Allocation failed!\n");
Exit (1);
}

Related Article

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.