Does ‘delete’ really delete the specified memory block?

exploreIT
3 min readAug 11, 2021

--

The answer is NO! ‘delete’ doesn’t delete anything.

Hmm… you may think HOWWW…!

This is basically one of the Undefined Behavior of C++ (Well, if you don’t know what is undefined behavior and why it is allowed in some of the programming languages please go here).

Before moving into the explanation let me tell you what ‘delete’ is?

delete: delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression.

Well, thanks for the definition but we want the answer of the above ‘HOWWW…!’

Hold on, hold on! I’m gonna explain that :)

Try to execute the below code and see what results are coming-

We are still able to access a data even after deleting it using ‘delete’

Output:

100

100

Oh no! we got the previous data which we deleted (Note that, it’s an undefined behavior, you may not get the same output as it is compiler dependent. Check this for more info!)

Basically we say “delete is used to destroy array and non-array(pointer) objects which are created by new expression”, but how the destruction is done?

Let us have a deep dive into the destruction process-

When we create a memory block using ‘new’ keyword the specified block is allocated in the heap memory. Now if we call delete what happens is that- the memory is marked as available, so the next time someone does new, the memory may be used. But it is not deleted. You see I said “delete doesn’t delete anything”, so what it does is, it just marks the memory as “being free for reuse”. Until some other allocation call reserves and fills that space it will have the old data. However, you should not rely on that data, basically if you delete something should just forget about it!

Yessss…! this is the reason you were getting that output while you were running the code. But as I mentioned this is one the Undefined Behaviors in C++ you may or may not be able to access the data and of course you should not rely on that data which is said to be freed.

If you think about it, it’s logical also- when you tell the compiler that you are no longer interested in the memory (using delete), why should the computer spend time on zeroing it? Isn’t it?

One of the practices in this regard that is often encountered in libraries is a Delete function:

template< class T > void Delete( T*& pointer )

{

delete pointer;

pointer = NULL;

}

This prevents us from accidentally accessing invalid memory. Note that it is perfectly okay to call delete NULL;

Reference StackOverflow and Others

Hey, did you like the explanation?

If yes then don’t forget to follow exploreIT as I regularly come up with interesting topics related to programming. And of course, if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!

Feet free to connect — https://www.linkedin.com/in/salman-shaikh-82989b1b9/

See you in the next article, Bye Bye!

Related Article(s)

--

--

exploreIT
exploreIT

Written by exploreIT

This is Salman, a Software Developer. Here in exploreIT, I come up with interesting concepts related to programming that you may enjoy. Do check & have fun:)

No responses yet