Roman Byshko
Roman Byshko

Reputation: 9002

Should memory be freed just before return statement in main?

Consider this small program:

int main(int argc, const char *argv[])
{
    int *i = new int;
    //do something with i, where i is involved all the time
    return 0;
}

As a matter of good style and to show that you actually know you must free memory allocated on the heap you normally add delete i; just before return statement.

But will it do any harm if leaving delete i; out?

UPDATE: Please do not flame each other in comments. I do know that just to be on safe side and just as the matter of good style and so on I should free allocated memory. The question is, can it be ommitted safely in this particular case.

Upvotes: 4

Views: 1307

Answers (8)

Kijewski
Kijewski

Reputation: 26022

There won't be any harm. The OS deallocates all the process's memory, when it dies.

Nevertheless not deleting your variables is a bad style as it hinders the use of tools such as Valgrind.

Upvotes: 3

Pubby
Pubby

Reputation: 53017

Your memory will be freed on a modern OS, but that doesn't mean the destructors will be called.

It's a bad practice. Don't do it. Use smart pointers instead.

Upvotes: 2

ScarletAmaranth
ScarletAmaranth

Reputation: 5101

Memory shall be retrieved by most operating system and donated back to heap. But would you ever rely on that ? See i reckon your very specific example is set so that it looks innocent. Yes the program finishes and then operating system snatches back all memory you forgot to de-allocate. The only problem is that as soon as your program gets bigger and will serve whatever purpose and someone lets it running, then you're basically 'stealing' memory that could have been available for other services running.

a) Never waste system resources (memory, processing power, ports, sockets ... )

b) Return whatever you don't need, there are other processes running along with yours.

I reckon you modeled a scenario that basically relies on your application being terminated which does not necessarily have to be the case.

Upvotes: 1

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19339

It shouldn't matter, since modern operating systems tend to clean up a program's memory after the program terminates. That being said, I'm pretty sure some older systems don't do the clean up, so if you're targeting any of those systems then you'll definitely want to be careful.

Regardless of both those points, wouldn't you sleep better knowing that your code doesn't leak?

Upvotes: 5

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

If you use memory tracking tool to find memory leaks, unbalanced allocs/deallocs from your top level would show up as leaks. This will distract you from your task of finding the real memory leaks. Other than that, you are safe: OS will free up the memory and other resources for you.

Upvotes: 1

Kamyar Souri
Kamyar Souri

Reputation: 1923

If you run the code in debug mode, the allocated memory will be tracked differently and will be freed up at program termination. BUT if you run the program in release mode it causes memory leak and if you run your program over and over, ultimately all memory on system will be allocated and not freed up causing low memory and all other bad things.

so it is more than just good coding style.

Upvotes: -4

Jari666
Jari666

Reputation: 73

I think in this example it would not hurt, since the program will exit anyway and most operating systems should just make that memory available on the heap again. But you should better just put a delete statement after having used the variable.

Upvotes: 0

Daniel
Daniel

Reputation: 31559

No, it won't do any harm as the system cleans all of the non deleted ones automatically.
the harm will if you write code that excepts to use everything until the program terminates, and then you discover you need to run it multiple times and you will have memory leaks between iterations.

Upvotes: 4

Related Questions