1. new Operator and delete OperatorThis is the most commonly used new and delete, which we use to create and release objects in the heap. What we need to know is what they do internally:
// When we write this statementObject *object = new Object();// it actually does the following things//1. Create a heap space of size Objectvoid *memory = operator new(sizeof(Object));//2. Construct Object in the heap spaceObject() in memory;//3. Point the object pointer to that heap spaceObject *object = static_cast<Object*>(memory);
As we can see, new creates the heap space, constructs the object, and points the pointer to the heap. The construction of the object is something that is difficult for us programmers to do because we cannot directly call the object initialization.The internal operation of delete is even simpler, which is to destruct the object and release the heap:
// When we write this statementdelete object;// it actually does the following things//1. Destruct the objectobject->~Object();//2. Release the heap memoryoperator delete(object);
The above new operator and delete operator are the most commonly used operations, and they are also operators that cannot be overloaded. However, some operators they call can be overloaded.2. operator new and operator deleteThese two operators have already been seen in the internal workings of the new operator and delete operator. Their function is to allocate a block of memory in the heap and release that memory.operator new allows passing a size_t to set the size of the heap memory being allocated. They are allowed to be overloaded, although we generally do not use them:
class MyClass {public: // Overload the normal operator new static void* operator new(size_t size) { std::cout << "Custom new, size: " << size << std::endl; return ::operator new(size); } // Corresponding operator delete static void operator delete(void* ptr) { std::cout << "Custom delete" << std::endl; ::operator delete(ptr); }};
It can be seen that operator new and operator delete are very similar to malloc and free in C. However, it is important to note that operator delete can only release memory produced by operator new; releasing dynamically allocated memory produced by other means will result in an error.You may wonder what the use of operator new and operator delete is; in fact, they need to be used in conjunction with another special version of operator new called placement new.3. Placement newAs a special version of operator new, placement new can also be overloaded:
class MyClass {public: // Overload placement new static void* operator new(size_t size, void* ptr) { std::cout << "Custom placement new" << std::endl; return ptr; }};
By overloading the function, we can see that placement new actually adds an additional void* type parameter compared to operator new, and it will directly return void* without creating new memory. When we use it like this:
//1. Create a heap space of size Objectvoid *memory = operator new(sizeof(Object));//2. Use placement newObject *object = new(memory) Object();
We achieve the functionality of the new operator through placement new and operator new. (My article “C++ Programming Tips: Do Not Provide Default Empty Constructor Unless Necessary” mentioned specific usage scenarios.)The key point is the statement “new(memory) Object();” which is essentially the new operator, but it replaces the internally called operator new with placement new.This way, we can achieve the desired functionality without overloading the new operator and delete operator.4. new and delete for ArraysThe reason for placing the new and delete for arrays at the end is that the only difference from the above content is two points:
- Add square brackets when using: operator new[], operator delete[], new Object[10], delete[].
- The internal implementation of the construction and destruction process is a loop.