New and delete operator usages in C + + _c language

Source: Internet
Author: User

"New" is a keyword in C + + and is also an operator. When we use the keyword new to create an object dynamically on the heap, it actually does three things: get a chunk of memory, call the constructor, and return the correct pointer. Of course, if we create a simple type of variable, the second step is omitted.

New usage:

1. Open a single variable address space

1) New int;
Opens up a storage space for the array and returns an address to the storage space. int *a = new int means assigning an address of type int to integer pointer A.

2 int *a = new int (5) acts as above, but at the same time assigns an integer value of 5

2. Open array Space
One-dimensional: int *a = new INT[100]; Create an integer array space of size 100
Two-dimensional: int **a = new Int[5][6]
Three-dimensional and above, and so on.

General usage: New type (initial value)

Delete usage:

1. int *a = new int;
Delete A; Free space for a single int

2.int *a = new Int[5];
delete [] A; Frees int array space

To access the structure space opened by new, it cannot be done directly through the variable name and can only be accessed through the assigned pointer

C + + new operator and delete operator

So far, you have defined the variables you want to use, and when the program starts executing, the variables are automatically configured with memory space.
Sometimes, however, some variables do not know when they will be used, and you want to allocate space to the variable when it is used, and return the space the variable occupies to memory when the variable is not in use, at which point we can use the new operator and the delete operator.

As a simple example, you can configure an int type of memory in a dynamic way in the program, for example:
int *ptr = new int;

In this program, the new operator configures the space required for an int, and returns the address of the space, you use pointer ptr to store this address, which configures only space but does not initialize the stored values in the space, and you can define this if you want to specify a stored value after the configuration is complete:
int *ptr = new int (100);

After configuring the space, this program sets the storage value in the space to 100
Execution results:
Space location: 0x3d2458
Space Storage Value: 100
Space location: 0x3d2458
Space Storage Value: 200

Space configured dynamically using the new operator, it is not automatically returned to memory until the end of the program, and you must use Delete to return the space to memory, such as the action of the previous program at the end, in which the program ends, although the end is displayed, but here is the use of the model delete. And this is a good habit, in the future, if your program in the continuous execution of a large number of use of new without the proper use of delete, because the space has not been returned, will eventually lead to the entire memory space exhausted.

Similarly, the array space, which is configured with new, should be returned to memory when not in use by using Delete, as follows:
delete [] arr;

Note When using Delete to return the array space to memory, we must add [] to return the entire array space.

Copy Code code as follows:

The following uses a simple program to demonstrate the dynamic memory configuration and use of the heap

#include <iostream>
using namespace Std;

int main () {
int *ptr = new int (100);

cout << "Space position:" << ptr<< Endl;
cout << "Space Storage value:" << *ptr<< Endl;

*ptr = 200;

cout << "Space position:" << ptr<< Endl;
cout << "Space Storage value:" << *ptr<< Endl;

Delete ptr;

return 0;
}



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.