operator new dynamic variable basic memory management

Source: Internet
Author: User

1. Create a new dynamic variable of the specified type with the operator new, and return a pointer to the new variable.

int *p;
p=new int;

The C + + standard stipulates that if there is not enough memory to create a new variable, operator new will terminate the program by default.

The system retains a special memory area for the dynamic variable, which is called free storage. Any new dynamic variable created by the program consumes part of the memory in free storage. If your program creates too many dynamic variables, you may run out of all memory in free storage.

The operator Ndelete destroys a dynamic variable and returns the memory it occupies to free storage. This allows you to reuse those memory to create a new dynamic variable. For example, a statement can destroy a dynamic variable that the pointer variable p points to:

Variables created using the new operator are called dynamic variables because they are created and destroyed when the program is run. Compared to these dynamic variables, the normal variable seems to be static, but that's not the case. Think about the formal parameters of a function.

Delete p;
2. Virtual Suspension pointer

If a pointer variable points to a destroyed dynamic variable, that pointer also enters an undefined state. These undefined pointer variables are called virtual overhang pointers.

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.