On the usage of new and delete in C + + _c language

Source: Internet
Author: User

The new and delete operators are used to dynamically allocate and revoke memory operators

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 forth.

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, you cannot go directly through the variable name, only through the assigned pointer.

With new and delete can be dynamically opened, undo the address space. If you run out of a variable (typically a stored array) the next time you need to use it, but you want to omit the reinitialization, you can open up a space each time you start using it, and then undo it when you're done with it.

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.