• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When does an Object gets freed?

 
Ranch Hand
Posts: 112
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple question...

Let us say that an object has been allocated memory in a function. Now, when does that memory free up?

Is it something that gets freeded up as soon as the method exits or is it something that needs to be deallocated explicitly?..


P.S: This is not home work!..Im googling for answers too!.

Thanks,
Pavan.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pavan Kumar Dittakavi wrote:A simple question...

Let us say that an object has been allocated memory in a function. Now, when does that memory free up?

Is it something that gets freeded up as soon as the method exits or is it something that needs to be deallocated explicitly?..



Well, it depends. If it is a local variable, then it will be freed when it goes out of scope. If it is a variable that has been assigned via the new operator (ie. a pointer variable), then it will be freed when the delete operator is called.

Henry
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume, you are referring to C++. The memory used by any object is released when its destructor executes sucessfully.
 
author
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great question. It's basically been answered here....

(1) For local (that is, automatic variables, allocated on the stack), they are destroyed when the associated variable goes out of scope.

(2) If they are allocated with "new", then you must explicitly delete them.

(3) For global/static variables, the objects are destroyed when the program ends.

(4) Otherwise, all objects are supposed to be de-allocated (by default) when the program ends. I'm not sure you should always count on this, however. I would still take special care to explicitly delete objects allocated with "new."

Hope this helps,

Brian Overland
 
reply
    Bookmark Topic Watch Topic
  • New Topic