Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ os memory
I have a question or rather a curiosity to know..
 
In following program, Do i need to free the allocated memory ??
 

 
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 7-Feb-13 2:59am
Comments
Jiří Miklík at 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 at 7-Feb-13 15:40pm
   
"Then why should i care about this ??"
Beacuse you are are a C++ programmer, not a managed coder.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100