Main Content

Missing overload of allocation or deallocation function

Only one function in an allocation-deallocation function pair is overloaded

Description

This defect occurs when you overload operator new but do not overload the corresponding operator delete, or vice versa.

Risk

You typically overload operator new to perform some bookkeeping in addition to allocating memory on the free store. Unless you overload the corresponding operator delete, it is likely that you omitted some corresponding bookkeeping when deallocating the memory.

The defect can also indicate a coding error. For instance, you overloaded the placement form of operator new[]:

void *operator new[](std::size_t count, void *ptr);
but the non-placement form of operator delete[]:
void operator delete[](void *ptr);
instead of the placement form:
void operator delete[](void *ptr, void *p );

Fix

When overloading operator new, make sure that you overload the corresponding operator delete in the same scope, and vice versa.

For instance, in a class, if you overload the placement form of operator new:

class MyClass {
   void* operator new  ( std::size_t count, void* ptr ){
   ...
   }
};
Make sure that you also overload the placement form of operator delete:
class MyClass {
   void operator delete  ( void* ptr, void* place ){
   ...
   }
};

To find the operator delete corresponding to an operator new, see the reference pages for operator new and operator delete.

Examples

expand all

#include <new>
#include <cstdlib>

int global_store;

void update_bookkeeping(void *allocated_ptr, bool alloc) {
   if(alloc) 
      global_store++;
   else
      global_store--;
}


void *operator new(std::size_t size, const std::nothrow_t& tag);
void *operator new(std::size_t size, const std::nothrow_t& tag) {
    void *ptr = (void*)malloc(size);
    if (ptr != nullptr)
        update_bookkeeping(ptr, true);
    return ptr;
}

void operator delete[](void *ptr, const std::nothrow_t& tag);
void operator delete[](void* ptr, const std::nothrow_t& tag) {
    update_bookkeeping(ptr, false);
    free(ptr); 
}

In this example, the operators operator new and operator delete[] are overloaded but there are no overloads of the corresponding operator delete and operator new[] operators.

The overload of operator new calls a function update_bookkeeping to change the value of a global variable global_store. If the default operator delete is called, this global variable is unaffected, which might defy developer's expectations.

Correction — Overload the Correct Form of operator delete

If you want to overload operator new, overload the corresponding form of operator delete in the same scope.

#include <new>
#include <cstdlib>

int global_store;

void update_bookkeeping(void *allocated_ptr, bool alloc) {
   if(alloc) 
      global_store++;
   else
      global_store--;
}


void *operator new(std::size_t size, const std::nothrow_t& tag);
void *operator new(std::size_t size, const std::nothrow_t& tag) {
    void *ptr = (void*)malloc(size);
    if (ptr != nullptr)
        update_bookkeeping(ptr, true);
    return ptr;
}

void operator delete(void *ptr, const std::nothrow_t& tag);
void operator delete(void* ptr, const std::nothrow_t& tag) {
    update_bookkeeping(ptr, false);
    free(ptr); 
}

Result Information

Group: Good practice
Language: C++
Default: Off
Command-Line Syntax: MISSING_OVERLOAD_NEW_DELETE_PAIR
Impact: Low

Version History

Introduced in R2019a