006--c++ Dynamic Memory (Introduction)

Source: Internet
Author: User

(1) Use new to obtain and specify a common format for allocating memory for a data object:

TypeName * Pointer_name=new typeName;

(2) Use Delete to free up memory

Delete Pointer_name;

It is important to note that delete frees the memory of the data object that the Pointer_name line is in. The Delete also applies to releasing the memory requested with new.

The C + + standard states that you do not attempt to free memory that has been freed, and you cannot use Delete to free the memory that is obtained by declaring the variable.

int age=23;

int * page=&age;

Delete page; This is an illegal operation.

  Note: the key to using delete is to use it for the memory allocated by new. This does not mean that you want to use the pointer for new, but rather the address of new.

int * Ps=new int;

int * PQ=PS;

Delete PQ;

In general, do not create two pointers to the same block of memory, as this will increase the likelihood of the error of deleting the same memory block two times.

006--c++ Dynamic Memory (Introduction)

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.