Memory Allocation and release in C and C ++-malloc, free, new, delete

Source: Internet
Author: User

1. New execution process: (1) applying for memory through operator new (2) Calling constructor using placement new (ignore this step for simple type) (3) Returning memory pointer

2. comparison between new and malloc: (1) new_handler will be called when new fails, but malloc will not. If new fails, null will be returned. (2) New can automatically call the object's constructor through placement new, malloc won't (3) New stuff comes out of the class, malloc is void *, need to force conversion (4) New is C ++ operator, malloc is c Standard Library Function

3. Delete execution process: (1) Call The Destructor (ignore this step for simple types) (2) release the memory

4. Comparison between Delete and free (1) Delete can automatically call the object's destructor. malloc won't (2) Delete is a C ++ operator, and free is a C standard library function.

5. three new forms: new operator, operator new, placement new (1) New operator the new mentioned above is new operator, which consists of three steps (applying for memory, calling constructors, return memory pointer). For memory application steps, the new operator (operator new) is used. For the constructors called, it is up to placement new. (2) operator new can be overloaded like normal operators. Operator new applies for memory and calls new_handler for processing when the application fails. This is a loop process, if new_handler does not throw an exception, it will continuously apply for memory until it is successful. Overload operator New:

 

Class test {
Public:
Void * operator new (size_t size ){...}
};

Operator new will apply for memory by default. If it succeeds, the memory address will be returned. If it fails, new_handler will be called, and then the memory will be applied for again, and the loop will continue. Therefore, operator new must meet the following conditions to return:. some memory is reserved when the program starts, and the memory is released in new_handler, so that operator new can be allocated to Memory B. throw the bad_alloc exception C. directly exit the program (abort, exit) d. set the new new_handler handler. set_new_handler (0) cancels the current handler. The bad_alloc exception is thrown by default. (3) Placement new is used to locate the constructor, construct an object with the specified type of constructor on the specified memory address. Example: New (PTR) test ("hello"); // PTR-> test: Test ("hello ");

We can use malloc + placement new to build our memory management module. When creating an object, we can apply for a memory block through malloc, and then call placement new to initialize the object. When releasing the object, first, call the destructor of the object, and then release the space through free.

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.