Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

If I dynamically allocate memory to a variable as shown below and suppose that I am not deallocating the memory. Then how much time it(the allocated memory) will reside in the system. Will it release the memory when I close the application.? Can any one clear my doubts.?

main()
{
int *ptr;
ptr=new sizeof(int);
}
Posted

Arun, I just want to add a note to the correct answer by C Pallini.

Even though you should not worry about releasing of memory on application termination (it will also be released, as most of other resources), you should always release your heap memory on every step.

One obvious reason is preventing memory leaks (http://en.wikipedia.org/wiki/Memory_leak[^]).

Another reason should make you taking care even about the objects allocating memory only once. In practice, a failure to release memory would not affect the behavior of your application. However, it would be bad for maintainability of your code. It might happen that latter on you will need to fight a memory leak or use some memory debugger by any reasons; so the memory debugger will detect false positives for you, effectively preventing to find "real" memory leaks.

A memory debugger can be a dear friend to you, don't betray it by lousy or careless memory management. About memory debuggers: http://en.wikipedia.org/wiki/Memory_debugger[^].

—SA
 
Share this answer
 
v3
Comments
Arun India 29-Jun-11 0:06am    
Thank you very much SAKryukov. This is the answer which I expect.My5
Sergey Alexandrovich Kryukov 29-Jun-11 0:54am    
I tried hard :-)
In this way, as you asked about lifetime, it is defined by the moment of allocation and deallocation; (new and delete for instances of classes) and, in case of lack of deallocation, by the termination of ***process*** -- in those OS handling memory nicely (see answer by Nimanja and my comment).

Thanks for accepting the answer.
Good luck, call again.
--SA
Strictly speaking, it depends on the OS. Windows and most (all?) Unix systems will clean up the memory used by the process when it exits. However, there are environments where it does not happen.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Jun-11 0:47am    
Frankly, I don't know such environments but can easily imagine their existence (quite sloppy ones). So, formally you're right: this is a feature of OS and nothing like C++'s. I voted 5 for this note.
--SA
Yes, it will remain allocated until the application terminates (when the process is closed, the system 'cleans up' its resources).
 
Share this answer
 
v2
Comments
goorley 28-Jun-11 5:32am    
You cannot use the term 'resources'. Not all resources are automatically cleaned up after the process terminates, but memory allocations are.
CPallini 28-Jun-11 5:40am    
Most of them are automatically removed. For instance files are closed, serial communication ports released and so on.
goorley 28-Jun-11 7:43am    
Database connection handles are not always automatically recoverd. At least not for Oracle.
CPallini 28-Jun-11 7:50am    
'Most' means 'most', I guess.
Sergey Alexandrovich Kryukov 28-Jun-11 13:11pm    
You're absolutely right. How anyone could argue against it under the "most" clause?
By the way, a 5 for the answer.
--SA
Please note that the correct syntax to allocate an integer is
new int

and not
new sizeof(int)

as you did.

And ... yes, the most of OSes (all windows and unix derived - including linux) deallocate all the memory a process allocates when the process terminates.
But you have to manage memory deallocation yourself, if you don't want to exhaust the memory of your system, for example calling continuously new into a loop, and never deleting the anymore used data.
 
Share this answer
 
You have to de-allocate the memory, otherwise, even you close the application, the memory is not released.
 
Share this answer
 
v2
Comments
PrafullaVedante 28-Jun-11 4:38am    
This is not correct. Memory will be freed once the process terminates.
Please refer Inside Windows NT book for detailed information.
Sergey Alexandrovich Kryukov 28-Jun-11 13:15pm    
I confirm, you're right. This answer tell something which is not true.
--SA
Arun India 28-Jun-11 8:06am    
Suppose if the application terminates because of some runtime error, then will it release the memory that is already allocated(Dynamically)?
Sergey Alexandrovich Kryukov 28-Jun-11 13:15pm    
Run-time error? It will ***of course*** be releases of process termination; not matter if it was terminated due to unhandled exception or by regular code -- it will be released in all cases, not exclusions.
--SA
Richard MacCutchan 28-Jun-11 9:42am    
Wrong!

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