0

Is it necessary to use desturctors in c++ and what really happens if I don't use desturctors after creating my class.

7th Apr 2018, 1:44 PM
shashuri Magrease
shashuri Magrease - avatar
20 Antworten
+ 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.
7th Apr 2018, 1:49 PM
Timon Paßlick
+ 4
Golden rule: Make sure that everything on the heap gets deleted. Pointer wrapper classes and deconstructors help you the most to ensure that.
7th Apr 2018, 5:48 PM
Timon Paßlick
+ 3
Yes, because when a raw pointer is deconstructed, its underlying data is not deleted.
7th Apr 2018, 1:53 PM
Timon Paßlick
+ 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.
7th Apr 2018, 5:17 PM
Timon Paßlick
+ 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.
7th Apr 2018, 5:37 PM
Timon Paßlick
+ 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.
7th Apr 2018, 5:56 PM
Timon Paßlick
+ 3
That's just to work with old libraries, though, you should try to use these pointer wrapper classes as often as possible.
7th Apr 2018, 5:58 PM
Timon Paßlick
+ 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.
7th Apr 2018, 6:04 PM
Timon Paßlick
+ 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
7th Apr 2018, 2:30 PM
Alex
Alex - avatar
+ 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
7th Apr 2018, 3:13 PM
Arun Tomar
Arun Tomar - avatar
+ 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
7th Apr 2018, 5:45 PM
Timon Paßlick
+ 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
7th Apr 2018, 5:45 PM
Timon Paßlick
+ 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.
7th Apr 2018, 6:00 PM
shashuri Magrease
shashuri Magrease - avatar
+ 2
Indeed.
7th Apr 2018, 6:08 PM
shashuri Magrease
shashuri Magrease - avatar
+ 1
I see; thanks!
7th Apr 2018, 2:01 PM
shashuri Magrease
shashuri Magrease - avatar
+ 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?
7th Apr 2018, 5:09 PM
shashuri Magrease
shashuri Magrease - avatar
+ 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!!!
7th Apr 2018, 5:52 PM
shashuri Magrease
shashuri Magrease - avatar
0
So in that case the class will not be closed and memory will still be used?
7th Apr 2018, 1:52 PM
shashuri Magrease
shashuri Magrease - avatar
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?
7th Apr 2018, 5:29 PM
shashuri Magrease
shashuri Magrease - avatar
0
I see, i will keep that in mind. This might be more complicated than I thought.
7th Apr 2018, 5:41 PM
shashuri Magrease
shashuri Magrease - avatar