0
Is it necessary to use desturctors in c++ and what really happens if I don't use desturctors after creating my class.
20 odpowiedzi
+ 4
The compiler generates a standard constructor which calls the destructors for all of your member variables.
But, for example with a raw pointer, that's not enough.
+ 4
Golden rule: Make sure that everything on the heap gets deleted.
Pointer wrapper classes and deconstructors help you the most to ensure that.
+ 3
Yes, because when a raw pointer is deconstructed, its underlying data is not deleted.
+ 3
Because when the program exits, all stack destructors are called. But A#4 and A#7 are on the heap.
However, after a program has exited, all of its memory is returned to the OS.
So these 2 variables don't consume heap space anymore after exit, but they were never deconstructed.
+ 3
No, you don't return it like you think. It's just that often, the OS gave you some limited heap area. It remembers the end of your area. Now, when the program exits, it says: Ok, this area isn't needed anymore. And then, it can give it away again. But the destructors for the 2 variables are never called.
I forgot to point out that it's not guaranteed that the OS does that. The memory could also be leaked until you reboot.
+ 3
Thank you, I don't know how many people had it before, but I came up with it. So it means a lot to me that you like it.
+ 3
That's just to work with old libraries, though, you should try to use these pointer wrapper classes as often as possible.
+ 3
C++ offers you strong enough tools to have no memory leaks at all.
And that's really important, otherwise your system will crash after running for a long time because it's out of heap.
+ 2
I made a code for this question some days ago. It shows in which conditions which kind of elements get deleted properly.
It's not so we'll documented though (I'm going to modify it soon to explain it a bit more.)
If you have questions just ask.
https://code.sololearn.com/cmyMGfNUFcMc/?ref=app
+ 2
According to me..
what i know..
main purpose for creat a constructor in c++.
is to initialised the object..
and for
destruction-->
is the last method that executive on every object..
before the user free the object....
that the only purpose for make of destructor to perform the last action on the object before free the object...
I try my best..
i hope you got what i try to say
+ 2
That's "bonus knowledge". Because if it's leaked while your program runs, that's bad enough.
Look also at this thread: https://www.sololearn.com/discuss/1200118/?ref=app
+ 2
That's "bonus knowledge". Because if it's leaked while your program runs, that's bad enough.
Look also at this thread: https://www.sololearn.com/discuss/1200118/?ref=app
+ 2
Got it. I will try to make a habit of it. I want my future projects to take as less memory as possible and to be highly optimized so I want to eliminate as much wasted memory as possible.
+ 2
Indeed.
+ 1
I see; thanks!
+ 1
@Alex I studied the code and seems to be following up but; I don't get why construct A# 4 and 7 weren't destroyed even after the program exits?
+ 1
@ Timon NICE!! I saw the thread man and it was a brilliant idea, we can use our destructor in our class to create a free() pointer function to prevent the leak, as a result whenever we're done with our class object the memory is freed. Makes more sense, thanks!!!
0
So in that case the class will not be closed and memory will still be used?
0
@ Timon Interesting, are you saying because values in the heap are not stored in a stack because it wasn't implemented by a means of a pointer and are therefore returned to the OS just like the maim function also returns values to it? And as a result the need for it to be destroyed is no longer called for?
0
I see, i will keep that in mind. This might be more complicated than I thought.