Restrict dynamic deletion of objects created on stack

Many times we encounter a situation where we need to delete a pointer but we are not sure that how this pointer is created i.e. either on Heap or on stack.

Check out this function,

class Dummy;

void deletePointer(Dummy * ptr)
  // Do you know this passed pointer was
  // created on heap or stack ???
  delete ptr;

In this it’s receiving a pointer and deleting it. But wait a minute….

What is this passed pointer is created on stack instead of heap ?

Dummy obj;
Dummy * dPtr = &obj;


In this type of case our application will crash.

Can we do something about it? Can we add a restriction in my class, so that pointer of object created on Stack can not be deleted dynamically i.e. by delete operator?


Yes we can do this.

Adding restriction on deletion of objects created on stack through delete operator

Mark each object created on heap and for other objects this marking will return false.

For this we will require following items in our class

  • Each Object will contain a member variable that will tell that current object is created on heap or not.
  • A static member variable in class that we will set in new operator and constructor

How This will work,

Each time we create an object on heap first operator new is called to allocate the memory and then its constructor is called. So, overload the new operator and add mark that new operator is called. Then in constructor check if new opeartor mark is true it means the current object is created on heap, so just update a member variable i.e. Heap marking for this current object. Afterwards reset the new operator Mask.

[showads ad=inside_post]


Now we have a member function that can tell if current object is created on heap or not.

Now overload the delete operator and before calling global operator on passed pointer check if it was created on heap or not.

Checkout following example.

#include <iostream>
#include <cstdlib>
#include <new>

class Dummy
	// Track the last call of new operator
	static bool m_islastCallOnHeap;

	// Tells if current object is created on
	// Heap or not
	bool m_isOnHeap;

		// Set the Heap flag for current Object
		m_isOnHeap = m_islastCallOnHeap;

		// reset the last call mark of new operator
		m_islastCallOnHeap = false;
	static void* operator new(size_t sz)
		std::cout << "operator new called\n"; // Current Object will be created on heap
		m_islastCallOnHeap = true; // Call the global new operator

		return ::operator new(sz);
	static void operator delete(void* ptr)
		Dummy * resPtr = (Dummy *) (ptr);
		// Before deleting check if passed object pointer
		// was created on heap or not.
		if (resPtr->m_isOnHeap)
			::operator delete(ptr);
			std::cout << "delete successful\n";
					<< "Sorry Dude.. This Object is not allocated from Heap\n";
	bool isOnHeap()
		return m_isOnHeap;


bool Dummy::m_islastCallOnHeap = false;

int main()

	Dummy resObj;
	Dummy * resPtr_2 = &resObj;

	// Deleting the Object created on Stack
	// through delete operator
	delete resPtr_2;

	return 0;

Output :

Sorry Dude.. This Object is not allocated from Heap

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

1 thought on “Restrict dynamic deletion of objects created on stack”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top