Delete object, necessaray??
-
Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.
yes. if there's no variable assigned to it? more so in that case ... me think that if you have allocated objects hanging around, you would need to clean them up. how do you have them ? calling a function that allocates something and return it and you don't use it ?
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.
if you use new you call delete ALWAYS. Even better would be to do this:
Object *x = new Object(); //... do things with x delete x; x = NULL;
This way you do not accidentally call x that points to something else than Object. (because the memory to which the pointer points to is overwritten) good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix -
Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.
-
Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?
-
Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?
It seems like you need to do some reading on memory allocation in C / C++ When you declare an object like this:- ObjectA theObject; It is created for you, and it will be removed automatically when it goes out of scope. When you declare an object like this:- ObjectA *thePointerToObject = new ObjectA(); You are doing three things:- creating a pointer (which will be removed for you), creating a new instance of ObjectA (which it is your duty to clean up), and setting your new pointer to point to the new object. This really is basic C++. *Any* C++ tutorial book/site will explain this; go out there are read up!
using System.Beer;
-
if you use new you call delete ALWAYS. Even better would be to do this:
Object *x = new Object(); //... do things with x delete x; x = NULL;
This way you do not accidentally call x that points to something else than Object. (because the memory to which the pointer points to is overwritten) good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix -
Hi all, Is it necessary to delete an object by calling delete object; or the object will be automatically deleted if there's no variable assigned to it? Thanks in advance for your help.
you should never wonder if the system will perform such operation for you. if you own the object, then take use to delete it when you don't need it anymore. if one day you come to work on poor operating systems which don't do this for you, or worse, systems that don't have any OS, you will have serious lacks of memory !!! think of it...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
V. wrote: if you use new you call delete ALWAYS. wrong. Windows can do it itself, but that's a very bad way to program...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
ok I correct myself: If you're a good programmerand if you use new you call delete ALWAYS :-D "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
-
V. wrote: if you use new you call delete ALWAYS. wrong. Windows can do it itself, but that's a very bad way to program...
TOXCCT >>> GEII power
[toxcct][VisualCalc]toxcct wrote: Windows can do it itself... Only if the application exits or is terminated. Things like services are designed to run for long periods of time so a call to
delete
is a requirement.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Thank you for your help. Another related question.. if I have the class: class ObjectA { ObjectB b; } ObjectA a = new objectA(); When i detele ObjectA (delete a), do I have to delete ObjectB (delete b)? or is it implicitly deleted?
Stack-based variables, as opposed to heap-based variables (i.e., those created with
new
), are cleaned up when the variable goes out of scope.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow