Error analysis caused by space using Delete to release new[]

Source: Internet
Author: User

After a tortuous exploration, the problem is the bottom of the question.

We are all cautioned that new and delete,new[] and delete[] will appear in pairs. What will be sent if you use Delete to release new[] application space? as follows:

t* p = new T [1024];

.....//do Something

What happens to delete p;//?

I'll tell you first, if T is a base type (such as int, double, char), you will find that the program is still working correctly, and that the memory space is also properly freed. Its correct operation, so that many people on the delete[] and delete the difference between this end. It is no wonder that in the domestic blog analysis of the principle of delete and delete[] and delete release new[] Article less, a lot of related articles about this question.

If the code is this:

class a{    public:    int  m;     ~A ()    {}}; int Main () {    Anew a[1024x768];     Delete ptr;     return 0 ; }

You can test this code and test it under vs and g++ to see what happens. This will increase your experience with debugging bugs later in the program (which I have encountered once)

To understand this problem, we need to know what new and New[],delete and delete[] are doing.

No more talking about new and delete.

1.new encapsulates the malloc function in C, requests a piece of memory space, and if T is a user-defined class type, the object on this memory space will be initialized with the constructor of T.

2.delete encapsulates the free function in C, and if T is a user-defined class type, first calls the destructor of T, and then invokes the freed memory space that the object occupies.

New[] will be more complex:

New  T [1024x768]; Delete [] ptr;

We notice that no information is passed to the delete[] operator operator (operator) about the PTR pointer. The original C + + compiler version is a requirement for programmers to pass in arguments about the size of the pointer to space, while modern compilers accept it, but in most cases it is ignored [1]!

Since there is no incoming parameter, the compiler must have somehow learned that the PTR is pointing to the size of the space.

Yes, there are a number of ways to say something. In the initial development of C + +, there is an associative array method: t* ptr = new[s] After applying for a space, the compiler will place a fixed array in the memory somewhere in the size of PTR and ptr corresponding to the space S, the next time when calling Delete[], It will look for the PTR in this array that corresponds to S. [2]

This method, the modern compiler has rarely seen, because it is very slow.

Modern g++, VC, ICPC all use the information Head (additional information head block) method to manage. When new[] applies for memory space, it will request n bytes, which is the information header, where we have a PTR pointer to a low address offset n bytes (block = ptr-n). Each time delete[] is called, the compiler will go to this information header to find the requested memory information.

So, calling new[] gets something different than calling malloc to get something!

Now, let's analyze

A * ptr = new A [1024];

Delete ptr;

What happens when this code is in the delete:

First, as with delete, the compiler assigns a destructor to delete, which, in the form of a function pointer, calls the destructor to be used on the object that the PTR points to, apparently 1023 objects after PTR do not call the destructor. Because we are using delete instead of delete[]. Then, delete executes the free function and frees up the requested space.

But! The pointer that we get from malloc in new[] does not start with PTR, and our program cannot find the PTR pointer allocated by malloc, and it crashes! [3]

And why is the base type not a problem? The program runs well. Yes, this is the reason for the compiler. The compiler checks that if the base tyep,new[] operation omits the previous header, and the delete release new[] occurs later, the PTR of the free call is consistent with that of the malloc call. Of course, this is not necessarily, there may be some compilers, at some point, even if the base type is it to add head block it?!

So, if new and delete,new[] and delete[] are not paired, what happens, undefined!

See here, believe you already understand delete[] why to correspond to new[], and delete also can't and delete[] mixed reason.

Have any questions welcome message, Exchange.

[1] [2] in-depth exploration of the C + + object model

[3]http://stackoverflow.com/questions/8940090/is-it-safe-on-linux-to-mix-new-and-delete "LiKao" answer

Error analysis caused by space using Delete to release new[]

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.