Delete and delete [] and a description of heap memory allocation

Source: Internet
Author: User

1. Brief Introduction

This article mainly describes two points. First, for the basic data type, delete and delete [] are the same on the release array, at least the results are the same. Second, heap memory address allocation is unpredictable in most cases, and stack is easier to predict.

2. Delete and delete []

# Include <iostream>
Using namespace std;
Class AK {
Public:
AK (){
Cout <"make one ak" <endl;
}
~ AK (){
Cout <"destroy one ak" <endl;
}
};
Int main (){
Int * p = new int [10];
Cout <(size_t) p <endl; // output 4013520
Delete p; // delete [] p;
P = new int [10];
Cout <(size_t) p <endl; // output 4013520
Delete [] p;
P = 0;
//-------------------------
AK * pAK = new AK [10]; // output "make one ak" 10 times"
Cout <(size_t) pAK <endl; // output 4013572 delete pAK; // output 1 "destroy one ak" pAK = new AK [1000000000]; // output "make one ak" 10 times"
Cout <(size_t) pAK <endl; // output 4013596
Delete [] Pak; // output "destroy one AK" 10 times"
System ("pause ");
Return 0;
}

Pointer P is stored in the stack, and the opened space is in the heap. For the first time, the opened space address is 4013520. After the array is released with Delete, It is reopened for the second time, and the space address is still 4013520. This indicates that delete is equally valid for the release of the int array.
The pointer Pak is stored in the stack, and the opened space is in the heap. The address of the space opened for the first time is 4013572. After the array is released with Delete, It is reopened for the second time. The space address is 4013596. It indicates that the original space is not completely released, because only one destructor is called with delete for the first time, that is, one "destroy one AK" is output ".

3. It is difficult to predict the heap address.

# Include <iostream>
Using namespace STD;

Int main (){
Int * a = new int;
Int * B = new int;
Cout <(size_t) a <endl; // output 4013416
Cout <(size_t) B <endl; // output 4013520
System ("PAUSE ");
Return 0;
}

A and B are two int * s stored on the stack. The two int s are stored in the heap, and the two int s in the heap are actually 104 bytes different. Generally, if it is a stack, the address of two consecutive int values must be continuous.
The reason is that the implementation structure of the heap is a linked list. Each time we scan from the linked list header and find the memory block that meets the requirements, the system returns. The remaining small blocks continue to be put back to the linked list. When memory is released, therefore, the heap is fragmented, while the stack is a sequential storage structure, and there is no fragmentation. Therefore, the address in the heap is hard to predict. The heap linked list is a management structure that is very suitable for dynamic memory development. However, this is also one of the reasons for poor prediction of its addresses. One exception is that several objects are opened up, all objects are released, and the same objects are opened in the same order. The corresponding addresses are the same. For example: Open int, char, int [10], release them, and then open int, char, int [10] in sequence. the corresponding address must be the same, because after the chain table is released, the original situation is restored.

4. Summary

· For an array of basic data types, delete is the same as delete []. For an array of sub-defined data types, only delete [] is allowed.
· Heap is a linked-list storage structure. In most cases, the data opened by heap cannot be predicted.

5. Emotion

Delete, new, delete [], new [] operators do not know how they are actually implemented. Therefore, it is difficult to really understand why delete is also feasible for arrays of basic data types, the array of custom objects is not feasible. However, I have read a lot of articles on the internet, and I have not found any of them, so I don't have to worry about them. After all, many of C ++ are hidden to save you trouble, if you do not develop driver-level or system-level programs, you will not be able to use them.
All of the features described in this article are known and not known. There is no big difference between the use of new and delete, no one can find the address to open up space in the heap, but when looking at the C ++ code later, it may be a step closer to the truth, that's all.

6. Reference

Difference between delete and delete [] http://hi.baidu.com/shijg/blog/item/f1b24d16d4b4774920a4e987.html
Difference between delete and delete [] in C ++ http://jazka.blog.51cto.com/809003/230220

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.