Use new and delete [] for dynamic memory allocation and release

Source: Internet
Author: User

The new and delete operators are new operators in C ++. They provide dynamic storage allocation and release functions. It is equivalent to the C language functions malloc () and free (), but the performance is superior. Using new has the following advantages over using malloc:

(1) New automatically calculates the size of the type to be allocated without using the sizeof operator, which is easy to use and can avoid errors.

(2) It automatically returns the correct pointer type and does not require forced pointer type conversion.

(3) You can use new to initialize the allocated object.

Example:

(1), int * P;
P = new int [10]; // assign an integer array containing 10 Integers
Delete [] P; // Delete this array

(2) int * P;
P = new int (100); // dynamically allocates an integer and initializes Delete and delete []. The difference is that a delete is static and a delete is dynamic. This article references:

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.