By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,390 Members | 2,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,390 IT Pros & Developers. It's quick & easy.

creating a temporary object using dynamically created object

P: n/a
Hello,

If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary. Then,
I assign this temporary object to "b", so that b's pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed. I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.

Thanks,
Jess

May 17 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Jess wrote:
If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary.
Right.
Then,
I assign this temporary object to "b", so that b's pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed.
But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.
No, it's still alive and well.

Your example is a good illustration of lifetime differences and what is
known as "ownership". Essentially, nobody /owns/ the object 'A' you
create when you say 'new A'. The temporary 'B' doesn't own it, it only
has a pointer to that object. 'b' doesn't own it either, it only takes
the copy of the pointer from the temporary.

Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory leak".

If you decide that a 'B' object should take ownership of the 'A' object
passed to it [by the pointer], then you should (a) take care of deleting
the object in the 'B' destructor (which you didn't even define), and
(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because you
will need to set 's.p' to 0 upon transferring the ownership of 'p' value.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #2

P: n/a
On May 17, 8:40 am, Jess <w...@hotmail.comwrote:
Hello,

If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}

};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}

};

int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;

}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary. Then,
I assign this temporary object to "b", so that b's pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed.
Temporary is destroyed but p is still pointing to undeleted memory. So
no harm.
In this case it will be problematic if you write destructor like this
~B(){delete p;}
I tried to output
the value of "b.p->x", and it still has the value 10.
As it should
However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.
NO
>
Thanks,
Jess

May 17 '07 #3

P: n/a
On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jess wrote:
If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.
I have the following program:
#include<iostream>
using namespace std;
class A{
public:
int x;
A():x(10){}
};
class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary.

Right.
Then,
I assign this temporary object to "b", so that b's pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed.

But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.

No, it's still alive and well.

Your example is a good illustration of lifetime differences and what is
known as "ownership". Essentially, nobody /owns/ the object 'A' you
create when you say 'new A'. The temporary 'B' doesn't own it, it only
has a pointer to that object. 'b' doesn't own it either, it only takes
the copy of the pointer from the temporary.

Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory leak".

If you decide that a 'B' object should take ownership of the 'A' object
passed to it [by the pointer], then you should (a) take care of deleting
the object in the 'B' destructor (which you didn't even define), and
Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.
(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because you
will need to set 's.p' to 0 upon transferring the ownership of 'p' value.
Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

May 17 '07 #4

P: n/a
siddhu wrote:
On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Jess wrote:
>>If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.
>>I have the following program:
>>#include<iostream>
>>using namespace std;
>>class A{
public:
int x;
A():x(10){}
};
>>class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
>>int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
>>In the "main" function, I first created an A object using "new",
then the A* pointer is converted to a B object, which is temporary.

Right.
>> Then,
I assign this temporary object to "b", so that b's pointer "p"
should also point to the dynamically created A object. After this
copying is done, the temporary B object should be destroyed.

But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
>> I tried to output
the value of "b.p->x", and it still has the value 10. However, it
may still mean the value is garbage, i.e. the dynamically created
object A object may have been destroyed.

No, it's still alive and well.

Your example is a good illustration of lifetime differences and what
is known as "ownership". Essentially, nobody /owns/ the object 'A'
you
create when you say 'new A'. The temporary 'B' doesn't own it, it
only
has a pointer to that object. 'b' doesn't own it either, it only
takes
the copy of the pointer from the temporary.

Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory
leak".

If you decide that a 'B' object should take ownership of the 'A'
object passed to it [by the pointer], then you should (a) take care
of deleting the object in the 'B' destructor (which you didn't even
define), and

Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.
Not if the ownership is correctly transferred.
>
>(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because
you
will need to set 's.p' to 0 upon transferring the ownership of 'p'
value.

Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.
That's true. But the pointer can be made 'mutable' specifically for
this situation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #5

P: n/a
On May 17, 2:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
siddhu wrote:
On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jess wrote:
If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.
>I have the following program:
>#include<iostream>
>using namespace std;
>class A{
public:
int x;
A():x(10){}
};
>class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
>int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
>In the "main" function, I first created an A object using "new",
then the A* pointer is converted to a B object, which is temporary.
Right.
> Then,
I assign this temporary object to "b", so that b's pointer "p"
should also point to the dynamically created A object. After this
copying is done, the temporary B object should be destroyed.
But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
> I tried to output
the value of "b.p->x", and it still has the value 10. However, it
may still mean the value is garbage, i.e. the dynamically created
object A object may have been destroyed.
No, it's still alive and well.
Your example is a good illustration of lifetime differences and what
is known as "ownership". Essentially, nobody /owns/ the object 'A'
you
create when you say 'new A'. The temporary 'B' doesn't own it, it
only
has a pointer to that object. 'b' doesn't own it either, it only
takes
the copy of the pointer from the temporary.
Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory
leak".
If you decide that a 'B' object should take ownership of the 'A'
object passed to it [by the pointer], then you should (a) take care
of deleting the object in the 'B' destructor (which you didn't even
define), and
Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.

Not if the ownership is correctly transferred.
OK. As I can guess you are talking about strict ownership. Are you
suggesting to do something like s.p=NULL; in the assignment operator?
>

(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because
you
will need to set 's.p' to 0 upon transferring the ownership of 'p'
value.
Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.

That's true. But the pointer can be made 'mutable' specifically for
this situation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

May 17 '07 #6

P: n/a
siddhu wrote:
On May 17, 2:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>siddhu wrote:
>>On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
Jess wrote:
If I create a temporary object using a dynamically created
object's pointer, then when the temporary object is destroyed,
will the dynamically created object be destroyed too? My guess
is that it's not destroyed, but I'm not sure.
>>>>I have the following program:
>>>>#include<iostream>
>>>>using namespace std;
>>>>class A{
public:
int x;
A():x(10){}
};
>>>>class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
>>>>int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
>>>>In the "main" function, I first created an A object using "new",
then the A* pointer is converted to a B object, which is
temporary.
>>>Right.
>>>> Then,
I assign this temporary object to "b", so that b's pointer "p"
should also point to the dynamically created A object. After this
copying is done, the temporary B object should be destroyed.
>>>But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
>>>> I tried to output
the value of "b.p->x", and it still has the value 10. However, it
may still mean the value is garbage, i.e. the dynamically created
object A object may have been destroyed.
>>>No, it's still alive and well.
>>>Your example is a good illustration of lifetime differences and
what is known as "ownership". Essentially, nobody /owns/ the
object 'A' you
create when you say 'new A'. The temporary 'B' doesn't own it, it
only
has a pointer to that object. 'b' doesn't own it either, it only
takes
the copy of the pointer from the temporary.
>>>Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory
leak".
>>>If you decide that a 'B' object should take ownership of the 'A'
object passed to it [by the pointer], then you should (a) take care
of deleting the object in the 'B' destructor (which you didn't even
define), and
>>Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.

Not if the ownership is correctly transferred.
OK. As I can guess you are talking about strict ownership. Are you
suggesting to do something like s.p=NULL; in the assignment operator?
Absolutely.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #7

P: n/a
On May 18, 4:29 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
siddhu wrote:
On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jess wrote:
If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.
>I have the following program:
>#include<iostream>
>using namespace std;
>class A{
public:
int x;
A():x(10){}
};
>class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
>int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
>In the "main" function, I first created an A object using "new",
then the A* pointer is converted to a B object, which is temporary.
Right.
> Then,
I assign this temporary object to "b", so that b's pointer "p"
should also point to the dynamically created A object. After this
copying is done, the temporary B object should be destroyed.
But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
> I tried to output
the value of "b.p->x", and it still has the value 10. However, it
may still mean the value is garbage, i.e. the dynamically created
object A object may have been destroyed.
No, it's still alive and well.
Your example is a good illustration of lifetime differences and what
is known as "ownership". Essentially, nobody /owns/ the object 'A'
you
create when you say 'new A'. The temporary 'B' doesn't own it, it
only
has a pointer to that object. 'b' doesn't own it either, it only
takes
the copy of the pointer from the temporary.
Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory
leak".
If you decide that a 'B' object should take ownership of the 'A'
object passed to it [by the pointer], then you should (a) take care
of deleting the object in the 'B' destructor (which you didn't even
define), and
Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.

Not if the ownership is correctly transferred.
By transfering the ownership, do you mean creating a new A object
(with the same value as the old A object) and assign it to the new
pointer, and then delete the old object and set the temporary B
object's pointer to NULL?
>

(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because
you
will need to set 's.p' to 0 upon transferring the ownership of 'p'
value.
Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.
Do you mean the non-const "b" object?
>
That's true. But the pointer can be made 'mutable' specifically for
this situation.
What is a mutable pointer and how does it help here?

Thanks,
Jess

May 18 '07 #8

P: n/a
On May 17, 1:40 pm, Jess <w...@hotmail.comwrote:
Hello,

If I create a temporary object using a dynamically created object's
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it's
not destroyed, but I'm not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}

};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}

};

int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;

}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary. Then,
I assign this temporary object to "b", so that b's pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed. I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.

Thanks,
Jess
You should definitley be using boost::share_ptr

May 18 '07 #9

P: n/a
Jess wrote:
On May 18, 4:29 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>siddhu wrote:
>>On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
Jess wrote:
If I create a temporary object using a dynamically created
object's pointer, then when the temporary object is destroyed,
will the dynamically created object be destroyed too? My guess
is that it's not destroyed, but I'm not sure.
>>>>I have the following program:
>>>>#include<iostream>
>>>>using namespace std;
>>>>class A{
public:
int x;
A():x(10){}
};
>>>>class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s's pointer, so they should point to the same
object
}
}
};
>>>>int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
>>>>In the "main" function, I first created an A object using "new",
then the A* pointer is converted to a B object, which is
temporary.
>>>Right.
>>>> Then,
I assign this temporary object to "b", so that b's pointer "p"
should also point to the dynamically created A object. After this
copying is done, the temporary B object should be destroyed.
>>>But 'b' retained that pointer, you copied it to 'b' yourself in the
assignment operator.
>>>> I tried to output
the value of "b.p->x", and it still has the value 10. However, it
may still mean the value is garbage, i.e. the dynamically created
object A object may have been destroyed.
>>>No, it's still alive and well.
>>>Your example is a good illustration of lifetime differences and
what is known as "ownership". Essentially, nobody /owns/ the
object 'A' you
create when you say 'new A'. The temporary 'B' doesn't own it, it
only
has a pointer to that object. 'b' doesn't own it either, it only
takes
the copy of the pointer from the temporary.
>>>Since nobody owns the 'A', nobody destroys it. The object lives on
until the program finishes executing. That's a kind of "memory
leak".
>>>If you decide that a 'B' object should take ownership of the 'A'
object passed to it [by the pointer], then you should (a) take care
of deleting the object in the 'B' destructor (which you didn't even
define), and
>>Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.

Not if the ownership is correctly transferred.

By transfering the ownership, do you mean creating a new A object
(with the same value as the old A object) and assign it to the new
pointer, and then delete the old object and set the temporary B
object's pointer to NULL?
>>

>>>(b) take care of the transfer of ownership in the assignment
operator which should most likely take a reference to non-const
'B' because you
will need to set 's.p' to 0 upon transferring the ownership of 'p'
value.
>>Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.

Do you mean the non-const "b" object?
A temporary is non-const. However, a reference to non-const (the
argument of 'operator=(B&)' ) cannot be bound to the temporary.
>That's true. But the pointer can be made 'mutable' specifically for
this situation.

What is a mutable pointer and how does it help here?
Not a mutable pointer, but a mutable member.

class B {
..
B& operator =(const B& s)
{
if (&s != this) {
delete p; // get rid of mine
p = s.p; // copy the other one's pointer
s.p = 0; // make 's' forget about its 'p'
}
}
..
mutable A* p; // mutable to be able to set it in the operator=
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 18 '07 #10

P: n/a
On May 17, 8:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
siddhu wrote:
On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[...]
(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const 'B' because
you
will need to set 's.p' to 0 upon transferring the ownership of 'p'
value.
Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.
That's true. But the pointer can be made 'mutable' specifically for
this situation.
Are you suggesting that assignment modify the value being
assigned from? There are cases where this might be justified,
but they are rare and far between. You're basically reinventing
auto_ptr, and we all know the design of auto_ptr is one of the
most complex issues in C++. A class which does this naively, as
you suggest, does not meet the Assignable requirements of the
standard containers, but it fails to meet them in a way that the
compiler will typically not detect (but that will cause subtle,
strange behavior down the road).

The general problem that Jess is that of object lifetime. It's
a problem which must be addressed at the design level,
regardless of the language involved. Only once you have decided
at the design level what the lifetime should be can you start
addressing the issue at the programming level.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.