![]() |
Can the member variable be accessed after the destructor is called explicitly?
Destructor is used to destroy(release) the Memory which is allocated to object. And it can be called explicitly. In the follwoing code the destructor for objA is called. After that the member of objA is accessed. How can it be possible if the destructor released the memory of objA?
Code: C
Output : in constructor a =10 in destructor a =10 in destructor |
Re: Can the member variable be accessed after the destructor is called explicitly?
Moved to C-C++ Forum
|
Re: Can the member variable be accessed after the destructor is called explicitly?
Quote:
Also depending on the implementation, destroying an object might not actually change the contents of RAM. So you could access memory after it has been freed, but not reliably because this memory is then available for other code to allocate. |
Re: Can the member variable be accessed after the destructor is called explicitly?
Quote:
You mean, when we call a destructor explicitly, the object is not freed !?!?! Is that true ? I think we can explicitly delete the objects by calling destructors. Quote:
Quote:
|
Re: Can the member variable be accessed after the destructor is called explicitly?
Quote:
Calling the destructor is one of those things you should only do if you REALLY KNOW what you are doing (and therefore don't need to ask). If you need to call the destructor, better to make the destructor call some function f() and call f() instead of the destructor. Anyway, if you want to delete the object, why would you want to call the destructor instead of using the perfectly good "delete object;"? Proof of concept code. If calling the destructor deletes an object then this should be an infinite loop: Code:
class fooChanged bar->~foo(); to delete bar; and the process remained tiny and took up 100% CPU instead of 100% RAM. *** Therefore calling a destructor DOES NOT free the object. *** POC2: accessing object memory after deleting the object: Code:
class fooThe code prints "bar->x='Hello'". But this is undefined behaviour, because bar has been freed. Without the dummies, building in Release mode I got bar->x='' and in Debug mode I got bar->x='¦¦¦¦¦¦¦¦¦¦¦¦¦¦' Interestingly, with dummies and building in Debug mode I got bar->x='¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦' But anyway, this is UB because of the delete. So this code is just to prove that you will get unpredictable results if you do stupid things. Moving the delete after the printf I got the same result in all four configurations (with and without the dummies, and in both Debug and Release mode): bar->x='Hello' |
Re: Can the member variable be accessed after the destructor is called explicitly?
Code:
|
Re: Can the member variable be accessed after the destructor is called explicitly?
> Error In TC = Member Identifier Expected...?
No idea. Code looks fine to me and even on Warnings Level 4 Visual Studio 2008 doesn't raise an eyebrow. Maybe you're using an old compiler that doesn't support that syntax? For some reason lots of people round here like using Borland C++ v3 from about 1836 even though the Microsoft compilers can be downloaded for free. |
Re: Can the member variable be accessed after the destructor is called explicitly?
Thanks all for ur reply. I just try this code to check whether calling destructor explicitly is doing the same job as the one provided by the compiler. So,to destroy an object the operator "delete" should be used (if a pointer to an object is created) and freeing up the memory allocated to the members can be done in the destructor. When the object goes out of scope it will not be no longer valid that time destructor will be called to do the operation mentioned, if any. Am I right?
|
Re: Can the member variable be accessed after the destructor is called explicitly?
The destructor is called when you delete the object or let it go out of scope, yes.
|
Re: Can the member variable be accessed after the destructor is called explicitly?
Note that if you explicitly call the destructor, and that frees memory, then you must make sure any pointers are set to zero so that when the destructor is called by delete the memory is not double freed. Double free can lead to undefined behaviour.
Best not to call destructors explicitly, really. I still haven't seen a good answer to why you would want to do that. |
| All times are GMT +5.5. The time now is 21:57. |