-
November 18th, 2018, 10:49 PM
#1
Destructor
I have got some doubt on destructor call.
When the object is deleted then the destructor is called.
When the object is deleted?
stack object when goest out of scope and heap object when we call a delete.
Please find the below code where object is allocated using new.
Code:
#include<iostream>
#include<vector>
using namespace std;
struct st{
st(int v):val(v){cout<<"st constructed for "<<val<<endl;}
~st(){cout<<"st desctructed for "<<val<<endl;}
int val;
};
int main()
{
{
st *p = new st(1000);
st dd = *(p);
}
cout<<"Before Program ends"<<endl;
}
Here i am not calling delete but assigned to a stack object and when i run the program i see the output as
st constructed for 1000
st desctructed for 1000
Before Program ends
so is the memory got de-allocated? Though i do not call delete.
-
November 19th, 2018, 12:03 AM
#2
Re: Destructor
It might be helpful if you printed the addresses of the objects as well, and implemented the copy constructor see it being invoked:
Code:
#include<iostream>
using namespace std;
struct st {
st(int v) : val(v) {
cout << "st constructed @ " << this << " for " << val << endl;
}
st(const st& other) : val(other.val) {
cout << "st copy constructed @ " << this << " from " << &other << " for " << val << endl;
}
~st() {
cout << "st @ " << this << " destroyed for " << val << endl;
}
int val;
};
int main()
{
{
st *p = new st(1000);
st dd = *(p);
}
cout << "Before Program ends" << endl;
}
Now I get:
Code:
st constructed @ 0x1486c20 for 1000
st copy constructed @ 0x7ffc5f0f4ae0 from 0x1486c20 for 1000
st @ 0x7ffc5f0f4ae0 destroyed for 1000
Before Program ends
so you can see that you constructed two st objects, one at 0x1486c20 and the other at 0x7ffc5f0f4ae0, but only the object at 0x7ffc5f0f4ae0 had the destructor invoked for it.
-
November 19th, 2018, 12:25 AM
#3
Re: Destructor
Thanks a lot. It is clear now
-
November 20th, 2018, 03:16 AM
#4
Re: Destructor
Originally Posted by raj1986
When the object is deleted?
stack object when goest out of scope and heap object when we call a delete.
There's a third option. As of C++ 11 a so called (reference counting) smart pointer became part of the standard. It's called std::shared_ptr.
This smart pointer is used like an ordinary pointer but is "smart" in the sense that it knows when the object it points to on the heap may be safely deleted. This happens when all copies of the smart pointer has gone out of scope so the heap object is no longer referenced from anywhere. Then the smart pointer deletes the object.
Last edited by wolle; November 20th, 2018 at 03:21 AM.
-
November 20th, 2018, 06:00 AM
#5
Re: Destructor
Originally Posted by wolle
There's a third option. As of C++ 11 a so called (reference counting) smart pointer became part of the standard. It's called std::shared_ptr.
This smart pointer is used like an ordinary pointer but is "smart" in the sense that it knows when the object it points to on the heap may be safely deleted. This happens when all copies of the smart pointer has gone out of scope so the heap object is no longer referenced from anywhere. Then the smart pointer deletes the object.
Or if the ownership of the memory doesn't need to be shared, then std::unique_ptr would be used. See http://www.cplusplus.com/reference/memory/unique_ptr/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|