C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Overloads new And delete Operators in C++

In this tutorial, we will learn how to overload the new and delete operators for a custom class in C++. Overloading these operators allows you to customize the memory allocation and deallocation for your class objects, which can be useful for optimizing performance or implementing custom memory management techniques.

Let's demonstrate overloading the new and delete operators using a simple MyString class as an example.

  • Create a simple MyString class:

First, let's create a basic MyString class with a constructor, destructor, and a function to display the string:

#include <iostream>
#include <cstring>

class MyString {
    char* str;

public:
    // Constructor
    MyString(const char* str) {
        this->str = new char[strlen(str) + 1];
        strcpy(this->str, str);
    }

    // Destructor
    ~MyString() {
        delete[] str;
    }

    // Function to display the string
    void display() const {
        std::cout << str << std::endl;
    }
};
  • Overload the new operator:

To overload the new operator for the MyString class, define a static member function with the following syntax:

static void* operator new(size_t size);

In the implementation, you should allocate memory of the requested size using a custom allocator or a global new operator and return the pointer to the allocated memory.

Here's the complete implementation for overloading the new operator:

// Overloading the new operator
static void* operator new(size_t size) {
    std::cout << "Overloaded new operator. Size: " << size << std::endl;
    return ::operator new(size);
}

Add the above code inside the MyString class to overload the new operator.

  • Overload the delete operator:

To overload the delete operator for the MyString class, define a static member function with the following syntax:

static void operator delete(void* ptr);

In the implementation, you should deallocate the memory pointed to by ptr using a custom deallocator or a global delete operator.

Here's the complete implementation for overloading the delete operator:

// Overloading the delete operator
static void operator delete(void* ptr) {
    std::cout << "Overloaded delete operator." << std::endl;
    ::operator delete(ptr);
}

Add the above code inside the MyString class to overload the delete operator.

  • Use the overloaded new and delete operators:

Now you can use the overloaded new and delete operators when allocating and deallocating objects of the MyString class:

int main() {
    MyString* str = new MyString("Hello, World!");
    str->display();

    delete str;

    return 0;
}

In this example, we create a MyString object using the overloaded new operator and then display the string. Afterward, we delete the object using the overloaded delete operator.

That's it for our tutorial on overloading the new and delete operators in C++. By overloading these operators, you can customize memory allocation and deallocation for your class objects to optimize performance or implement custom memory management techniques.

  1. How to Overload the new and delete Operators in C++:

    • To overload the memory allocation (new) and deallocation (delete) operators, you define global or class-specific versions.

    Example:

    void* operator new(size_t size) {
        // Custom memory allocation logic
        // ...
        return malloc(size);
    }
    
    void operator delete(void* ptr) {
        // Custom memory deallocation logic
        // ...
        free(ptr);
    }
    
  2. Custom Memory Allocation with new Operator Overloading in C++:

    • Overloading new allows custom memory allocation, useful for managing specialized memory pools or implementing custom allocation strategies.

    Example:

    class CustomAllocator {
    public:
        // Overloading new operator for custom memory allocation
        void* operator new(size_t size) {
            // Custom memory allocation logic
            // ...
            return malloc(size);
        }
    };
    
  3. Memory Deallocation with delete Operator Overloading in C++:

    • Overloading delete enables custom memory deallocation, useful for releasing resources or implementing custom cleanup procedures.

    Example:

    class CustomDeallocator {
    public:
        // Overloading delete operator for custom memory deallocation
        void operator delete(void* ptr) {
            // Custom memory deallocation logic
            // ...
            free(ptr);
        }
    };
    
  4. Overloading new and delete Operators for User-Defined Types in C++:

    • User-defined types can have their memory management behavior defined through new and delete operator overloading.

    Example:

    class CustomType {
    public:
        // Overloading new operator for custom memory allocation
        void* operator new(size_t size) {
            // Custom memory allocation logic
            // ...
            return malloc(size);
        }
    
        // Overloading delete operator for custom memory deallocation
        void operator delete(void* ptr) {
            // Custom memory deallocation logic
            // ...
            free(ptr);
        }
    };
    
  5. Placement new and Custom Memory Allocation in C++:

    • Placement new allows you to specify the memory location for an object. Overloading it enables custom placement.

    Example:

    class PlacementNew {
    public:
        // Overloading placement new operator for custom memory allocation
        void* operator new(size_t size, void* placementPtr) {
            // Custom placement new logic
            // ...
            return placementPtr;
        }
    };
    
  6. Global new and delete Operator Overloading in C++:

    • Global overloads apply to all user-defined types. Useful for implementing a global memory management policy.

    Example:

    void* operator new(size_t size) {
        // Global custom memory allocation logic
        // ...
        return malloc(size);
    }
    
    void operator delete(void* ptr) noexcept {
        // Global custom memory deallocation logic
        // ...
        free(ptr);
    }
    
  7. Overloading Array new and delete Operators in C++:

    • Overloading array versions allows custom memory management for arrays of objects.

    Example:

    class ArrayCustomType {
    public:
        // Overloading array new operator for custom memory allocation
        void* operator new[](size_t size) {
            // Custom array memory allocation logic
            // ...
            return malloc(size);
        }
    
        // Overloading array delete operator for custom memory deallocation
        void operator delete[](void* ptr) noexcept {
            // Custom array memory deallocation logic
            // ...
            free(ptr);
        }
    };