Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question or rather a curiosity to know..

In following program, Do i need to free the allocated memory ??



C++
int main()
{
  char * cpTest = new char[32];

  ...
  ...

  
}


Even if i do not free it , it will be reclaimed by the OS. Then why should i care about this ??
Posted
Comments
Jiří Miklík 7-Feb-13 8:32am    
Yes.
You are right. In that simply case you can't worry about memory.
But there are projects (or apps, if you want), where memory management is the most important thing.
And anorher point of view......
Meanwhile your greedy aplication is running, weak PC (with small amount of RAM) can have a problem.
JiMikl
CPallini 7-Feb-13 15:40pm    
"Then why should i care about this ??"
Beacuse you are are a C++ programmer, not a managed coder.

Because it is good practice.
Which means it's a good idea to get into the habit of freeing everything you allocate. If you do it every time, making sure that when you allocate something, you automatically set up the code to de-allocate it, then it becomes second nature and you don't foget when yoiu are working on larger projects.

If you think "it doesn't matter this time" and then later move the code into a function, will you add teh de-allocate code then? No - because you don't need it. Or even think about it. And nothing happens that causes a problem.

So when in March you want a function to do the same thing in a different project, you remember this function and copy'n'patse....bingo! it works!

Then in August, when you project has got huge and you have 100 users, and they start screaming at you because your app runs out of memory and crashes while they use it, will you remember then that the memory wasn't deallocated? Or have to search tediously though reams of code trying to work it out?

Do it now. Do it always. Never rely on the OS to clean up after you.
 
Share this answer
 
In this particular case there's no compelling reason to care but it's an extremely good habit to get into to free everything you allocate, using RAII where ever appropriate because 99.99% of the time not doing so will be an uncontrolled memory leak, a potential security risk, massive noise in any tool like valgrind that you're using to track down a bug and just generally bad practice. If you're an experienced developer who's stuff doesn't leak because he always cleans up then by all means trim the edges of your winding up code by not bothering with those last few global objects, you'll be in good company, Microsoft's own C runtime does the same.
 
Share this answer
 
v2
Hello,

Freeing objects along a program's life is not mandatory but recommended.
It is true that the OS will reclaim the memory used by your program whem it will terminate, however if your program allocates a lot of memory blocks while executing that could cause some "NotEnoughMemoryError" and might take you program down if you don't handle that kind of error.

The aim of freeing memory is not to give back memory to the system but first to your own program. Memory allocations are done in your program's virtual memory (up to 4G if compiled with x86 target, up to the system limit if compiled on x64 target), when your program runs out of virtual memory, it can't allocate objects anymore.

Now regarding the code snippet you posted I'd say you don't really need to free that memory straigth after discarding... but still that's goor practice and were you to develop against a real time system or a constrained system (say an embedded microsystem as an example) you would be obliged to free it as soon as you don't need it anymore.

Hope this makes sense to you.
 
Share this answer
 
Yes.
You are right. In that simply case you can't worry about memory.
But there are projects (or apps, if you want), where memory management is the most important thing.
And anorher point of view......
Meanwhile your greedy aplication is running, weak PC (with small amount of RAM) can have a problem.
JiMikl
 
Share this answer
 
Behind your simple question is a more involved query involving philosophy. You really should get into the habit of turning out the lights whenever you exit a room. The question you ask here is whether or not to turn out the lights after planting all the explosives, just before you implode the building.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900