Difference between creating an object with new and creating an object without new

Source: Internet
Author: User

There is a difference between creating objects with new in C ++ and creating objects without new. I wonder if you know exactly what the difference is? The following small series will use examples to tell you how to use them. If you need them, you can refer to them.

We all know that C ++ has three methods to create objects:

Copy codeThe Code is as follows:
# Include <iostream>
Using namespace std;

Class
{
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); // heap allocation
Delete c;
Return 0;
}


There is no difference between the first and second types. For an implicit call and an explicit call, both of them allocate memory in the stack of the virtual address space of the process, and the third type uses new, memory is allocated in the heap, and the allocation and release of memory in the stack are managed by the system. The allocation and release of memory in the heap must be manually released by the programmer, therefore, a problem arises: whether to put the object in the stack or in the stack. This problem is related to the difference between the stack and the stack itself:

There are several problems:
1. Maximum heap and stack memory size that can be allocated
2. Heap and stack memory management methods
3. Heap and stack allocation efficiency

First, to address the first problem, generally, the size of a process stack is much smaller than the heap size. in linux, you can use ulimit-s (unit: kb) to view the maximum allocable size of a process stack. Generally, it cannot exceed 8 MB, and some may not exceed 2 MB. However, you can set this parameter, the maximum allocable size of a process heap is in the order of GB. Different systems may be different. For example, a 32-bit system cannot exceed 2 GB, 64 is the maximum system size of 4 GB, so when you need a allocated size of memory, use new, that is, use heap.

Second, to address the second problem, the stack is the system data structure, which is unique for processes/Threads. Its Distribution and release are maintained by the operating system and do not need to be managed by developers. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's Instruction Set with high efficiency. different operating systems have certain stack restrictions. Heap memory allocation, also known as dynamic memory allocation. The program uses the memory applied by malloc during running. The programmer is responsible for managing the memory. The developer determines the lifetime of the program: When to allocate and how much to allocate, and when to use free to release the memory. This is the only memory that can be managed by developers. The performance and stability of the system are directly determined by the quality of use.

We can see from the above, but we need very little memory, you can determine the actual amount of memory you need, please use the stack. When You Need To Know How much memory you need at runtime, use the heap.

Finally, to address the third problem, the stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer: allocate a dedicated register to store the stack address, the output stack of the Pressure Stack has dedicated Command Execution, which determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated. For example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), 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 allocate enough memory, then return. Obviously, the heap efficiency is much lower than the stack efficiency.

We can see from the above that stack is used when stacks are used.

Copy codeThe Code is 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 ("Enter the total number of integers :");
Scanf ("% d", & n );
P = (int *) calloc (n, sizeof (int); // determines the memory allocation size during runtime
If (p = 0 ){
Printf ("failed to allocate! N ");
Exit (1 );
}

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.