creating a temporary object using dynamically created object

J

Jess

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
 
V

Victor Bazarov

Jess said:
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
 
S

siddhu

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
 
S

siddhu

Jess said:
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:

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

Victor Bazarov

siddhu said:
Jess said:
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:

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.
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
 
S

siddhu

siddhu said:
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?
 
V

Victor Bazarov

siddhu said:
siddhu said:
On May 17, 9:33 am, "Victor Bazarov" <[email protected]>
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:

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.
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
 
J

Jess

siddhu said:
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?

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
 
N

Nindi

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
 
V

Victor Bazarov

Jess said:
siddhu said:
On May 17, 9:33 am, "Victor Bazarov" <[email protected]>
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:

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.
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?

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.
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
 
J

James Kanze

[...]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top