C ++ delete usage

Source: Internet
Author: User
Tags shallow copy

In the previous article, we introduced some common methods of C ++ two-dimensional array new in detail. I believe you should have some knowledge. In this article, we can compare the functions of C ++ delete and have a full understanding of this knowledge.

  • Analysis of Different Methods of C ++ Memory Management
  • C ++ iterator
  • Detailed description of C ++ shallow copy
  • Interpretation of the C ++ callback function code example
  • Comments on several application methods of C ++ two-dimensional array new

New and C ++ delete operators are operators used to dynamically allocate and revoke memory

New usage:

1. Open a single variable address space

1) new int; // open up a bucket for storing arrays and return an address pointing to the bucket. int * a = new int: assign an int type address to integer pointer.

2) int * a = new int (5) serves the same purpose as above, but at the same time, the integer is assigned to 5

2. Open up array space

One-dimensional: int * a = new int [100]; opens up an integer array space of 100

Two-dimensional: int ** a = new int [5] [6]

3D and above: so on.

General Usage: new Type [initial value]

C ++ delete usage:

1. int * a = new int;

Delete a; // release the space of a single int

2. int * a = new int [5];

Delete [] a; // release the int array space

To access the struct space opened by new, the variable name cannot be used directly, but can only be accessed through the pointer of the value assignment.

New and C ++ delete can be used to dynamically open up and cancel the address space. when programming, if you use a variable (usually an array temporarily stored), you need to use it again next time, but want to save the effort of re-initialization, you can open up a space each time you start using it and undo it after it is used up.

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.