C ++ heap

Source: Internet
Author: User

Heap understanding:

The heap is a lot of discontinuous memory areas, which are connected by the list in the system. For data hiding, each unit of memory in the heap is anonymous, therefore, you must first apply for a memory unit address in the heap and save it in a pointer so that you can only access the content of the memory unit using the pointer.

Heap benefits:

1: Large Data Storage

2: programmers control creation and release by themselves

3: only a specific pointer can access specific data, thus avoiding any attempt to modify the data.

Practice:

First, create a heap and define a pointer to the heap. In this way, only the pointer can be used to access the data in the heap. In C ++, use the keyword new to create a heap and allocate a memory, the new object is followed by an object type to be allocated. The inexpensive device determines the size of the allocated memory based on this type.

Int * P; // defines a pointer variable to an integer.

P = new int; // new creates an int-type memory area in the heap, and assigns the memory pointer of this area to the pointer Variable P, in this case, P points to the new memory area.

New int // if it is successfully created, an address is returned, which is assigned to P.

It can also be merged into one sentence int * P = new int;

In this case, the pointer P is defined and its value is initialized as the memory address of an int-type storage area created in the heap.

We can use it like a normal pointer and assign the value to the memory space it points.

* P = 1;

 

Note that due to limited memory, there may be situations where there is not enough memory to meet the new request. In this case, new will return 0, after the value is assigned to the pointer, the pointer is a null pointer, which does not point to valid data.

 

Note the pointer Memory leakage:

Memory leakage: because the memory space created using new is not automatically released by the system, if you do not release it, the memory in this region will never be used by other data, the pointer pointing to the memory is a local variable. When the function defining the pointer ends and returns the result, the pointer disappears and we can no longer find the memory area, in this case, we call Memory leakage. Therefore, if you do not need a memory space, you must use the keyword Delete For the pointer to it.

Int * P = new int; Delete P; in this way, the memory pointed to by the pointer will be released, instead of the pointer, therefore, you can also use this pointer, and the memory in the heap to which the previous Pointer Points has been released.

Note: If the pointer points to a heap object, it can only be released once and cannot be released again. To release the pointer, P = 0. In this case, it is okay to release a null pointer.

The procedure is as follows:

Int * P = new int; // apply to the system for memory in an int-type heap to assign the returned address to the INT-type pointer p.

Delete P; // Delete the memory pointed to by the pointer P, but the pointer remains

P = new int; // apply for another heap memory to assign the returned address to the pointer P

Delete P; // Delete the memory that the Pointer Points.

P = 0; // set the pointer to a null pointer.

Delete P; // Delete the pointer. if the pointer is not set as a null pointer, the two Delete P values are incorrect.

Return 0;

 

/***************************/

Small memory leakage code:

Int * P = new int;

P = new int;

 

Correct practice:

In * P = new int;

Delete P;

P = new int;

Or

In * P1 = new int;

In * P2 = new int;

/***************************/

// Heap code explanation

Human * P; // defines a pointer to the human class.

P = new human; // creates a memory area and calls the default constructor of the human class to construct an object, the memory size occupied by it is determined by the member variables of the human object. Assume that the class has two member variables of the int type. the object occupies 2*4 = 8 bytes. After creating an object in the right half of the row, assign the memory address to the pointer Variable P on the left.

The above two rows can merge human * P = new human;

Delete P; // call the object's destructor and release the memory

 

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.