Placement new and placement Delete

Source: Internet
Author: User

I am writing the memory management module. I 'd like to ask you some questions.

First, I wrote a singleton management class for memory management:

Class memorymanagement ...{
Public:
Memorymanagement ();
~ Memorymanagement ();

Void initialize ();
Void finalize ();

Void * alloc (uint32 size );
Void free (void * P );
Void * alloc16 (uint32 size); // allocate 16 byte aligned memory
Void free16 (void * P); // free 16 byte aligned memory
......
} Extern memorymanagement gmem;

Then, the new and delete operators are reloaded to call this class. This uses placement New:

Inline
Void * operator new (size_t count, memorymanagement & MGR ){
Return Mgr. alloc (CX: uint32) count );
}

Inline
Void * operator new [] (size_t count, memorymanagement & MGR ){
Return Mgr. alloc (CX: uint32) count );
}

Inline
Void operator Delete (void * PTR, memorymanagement & MGR ){
Mgr. Free (PTR );
}

Inline
Void operator Delete [] (void * PTR, memorymanagement & MGR ){
Mgr. Free (PTR );
}

Now, we can use our own memory management object to allocate memory to create objects in this beautiful way:

Int32 * P = new (gmem) int32;
Int32 * P2 = new (gmem) int32 [10];

Now the problem is coming. How can we delete the objects we created like this? We have also defined the corresponding placement Delete operator, but it is not explicitly called. It is for the corresponding placement new. If the constructor throws an exception, it is designed to automatically call the corresponding placement Delete.

After research, we can find that for a delete that is not a vector, We can manually call the Destructor and then call our own memory release function:

Template <class T>
Void destroy (T * P, memorymanagement & MGR )...{
If (p )...{
P-> ~ T (); // explicit destructor call
Mgr. Free (P );
}
}

Then, we can easily delete our objects:

Destroy (p, gmem );

The final question comes. How can we correctly Delete the array of objects allocated by new?

How do C ++ know the number of new objects in Delete [] when it is in new [] and delete? Because, it needs to know this to correctly call the Destructor for each object. We can guess that in new [], he must place the number of objects somewhere, and then delete [] will get it. It is true that the code generated by the actual compiler is tracked. If you use new int [100], theoretically you need to allocate 400 bytes. In fact, it is allocated 404 bytes. Then, the four more bytes are used to store the requested memory size (the number of objects is incorrect in the past). This address is generally the first end of the allocated block, followed by the real object memory. Delete []. You can simply read this.

However, this is an internal feature of the C ++ language. How can we write a destroyarray similar to destroy to get this number and correctly analyze multiple objects allocated?

This is my problem.

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.