Main Content

CERT C++: DCL54-CPP

Overload allocation and deallocation functions as a pair in the same scope

Description

Rule Definition

Overload allocation and deallocation functions as a pair in the same scope.1

Polyspace Implementation

The rule checker checks for Missing overload of allocation or deallocation function.

Examples

expand all

Issue

Missing overload of allocation or deallocation function 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.

Example – Mismatch Between Overloaded operator new and operator delete
#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) { //Noncompliant
    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) { //Noncompliant
    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); 
}

Check Information

Group: 01. Declarations and Initialization (DCL)

Version History

Introduced in R2019a

expand all


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.