Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy C'tor - doubt

5 views
Skip to first unread message

AY

unread,
Nov 19, 2009, 6:04:48 PM11/19/09
to
I have a situation where I'm required to generate a copy of an object.
I have simplified the class into a tiny one, but in reality its a huge
class with multiple struct types as data member.

Here is my simplified class:

class AB
{
private:
int a;
public:

AB(int ar): a(ar){}

AB(const AB& arg)
{
a = arg.a;
}
};

int main()
{
AB* ab = new AB(7);

//AB* cd = new AB;

AB* cd(ab);

return 0;
}


My Qn is in the statement - AB* cd(ab);

Does object 'cd' uses the same memory as object 'ab' ?

Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
think, maybe I'm wrong ] ?

What happens here exactly.

Thanks in advance friends,
- AY

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

John H.

unread,
Nov 20, 2009, 4:46:26 AM11/20/09
to
On Nov 19, 5:04 pm, AY <techreposit...@gmail.com> wrote:
> int main()
> {
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);
>
> return 0;
>
> }
> My Qn is in the statement - AB* cd(ab);
> Does object 'cd' uses the same memory as object 'ab' ?
You have some confusion between objects and pointers to objects. I
think you could use an introductory C++ book, but I will take a stab
at your question:

Ex 1. The following creates an AB object:
AB ab(7);
This object is considered a local variable and lives on the stack. We
refer to this object by it's name, ab. When ab leaves scope, it will
be destroyed and memory will automatically freed from the stack.

Ex 2. The following creates an AB object:
new AB(7);
This AB object lives in the heap. It will stay in memory until the
you explicitly delete it. In that line, I didn't actually create any
way to access the AB object that is created, so this exact usage you
probably won't ever want to do.

Ex 3. The following creates a pointer that could point to an AB
object:
AB* ab;
This pointer is considered a local variable (and thus lives on the
stack). For now it doesn't point to anything useful.
No object of type AB is actually created. So as it stands, you can't
do anything with this pointer (until we have it point at an actual AB
object).

Ex 4. The following line creates an AB object and a pointer that
points to that AB object:


AB* ab = new AB(7);

Here the AB object is created as in example 2. Like the AB object in
2, it lives in the heap and will live there until you explicitly
delete it. Unlike example 2, we have also created a pointer named ab
that points to the object. This pointer is a local variable (stack).
With this pointer, we can access the object. The pointer will
disappear when it goes out of scope. However the AB object that is
created isn't tied to the life of the pointer that points to it, so it
can continue to live even after the initial pointer that pointed to it
goes away.

Ex 5. The following creates an AB pointer and another pointer that is
a copy of the first pointer:
AB* ab;
AB* cd(ab);
Here you have the example 3 scenario with ab. We have also created
another pointer called cd which you have told to be a copy of ab
pointer. This is a copy of the pointer itself, not a copy of whatever
ab was pointing to. Thus ab and cd point to the same "thing". In
this case, that thing is nothing useful. To get something useful,
read on...

> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?

No memory is created on the heap for cd. Like my example 3, it is
just a local variable, and some small amount of memory will be
allocated for it on the stack.

> What happens here exactly.
After your "AB* cd(ab);" code, you will have:
- An AB object on the heap
- A pointer that is a local variable (stack) called ab, which points
to the above mentioned AB object
- A pointer that is a local variable (stack) called cd, which points
to the above mentioned AB object
Note you are copying the pointer, not the actual AB object. Only one
AB object exists, with two way to access it. Your copy constructor is
never called. To invoke the copy constructor you could do something
like:


AB* ab = new AB(7);

AB* cd = new AB(ab);
Now we have:
- An AB object on the heap
- A pointer that is a local variable called ab, which points to the
above mentioned AB object
- A second AB object on the heap that is a copy of the first AB object
- A pointer that is a local variable called cd, which points to the
second AB object.
Now if you make changes to the first AB object by the ab pointer, you
will not see the changes made to the second AB object (pointed at by
cd) because they are two separate objects.

Chris Morley

unread,
Nov 20, 2009, 4:49:58 AM11/20/09
to
Hi,

> My Qn is in the statement - AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?
> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?

cd is a pointer which is copy of the ab pointer, so yes they point to the
same instance of the AB class. Your AB copy constructor is not called. No
more memory is allocated.

If you wanted to make a copy you need...
AB* ef = new AB(*ab);

With a few trace prints in your program you can easily see what is
happening. The output on my machine for the following complete program:

ab=0x6101a8, cd=0x6101a8
copy constructor: my this=0x6201c0, arg=0x6101a8
ab=0x6101a8, ef=0x6201c0
copy constructor: my this=0x22ccbc, arg=0x6101a8
ab=0x6101a8, gh=0x22ccbc

//======================
#include <stdio.h>

class AB {


int a;
public:
AB(int ar): a(ar){}
AB(const AB& arg) {

printf("copy constructor: my this=%p, arg=%p\n", this, &arg);
a = arg.a;
}
};

int main() {
AB* ab = new AB(7);
//AB* cd = new AB;

AB* cd(ab); // you have copied the pointer
printf("ab=%p, cd=%p\n",ab,cd);

AB* ef = new AB(*ab); // you have made a copy of the first AB
printf("ab=%p, ef=%p\n",ab,ef);

AB gh(*ab); // copy into local variable gh
printf("ab=%p, gh=%p\n",ab,&gh);

return 0;

Kwall Kuno

unread,
Nov 20, 2009, 4:48:54 AM11/20/09
to
On Thu, 19 Nov 2009 17:04:48 CST, AY wrote:

[snip]


> int main()
> {
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);
>
> return 0;
> }
>
>
> My Qn is in the statement - AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?

No.

> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?

No. Both 'ab' and 'cd', being automatic variables, are allocated on the
stack. They end up containing the same value, though.

> What happens here exactly.


AB* ab = new AB(7);

Here you declare a pointer 'ab';
then you allocate an object of type AB on the heap;
finally, you put the address of the newly-allocated AB object in 'ab'.
Now 'ab' points to an AB.

AB* cd(ab);
Here you declare another pointer, 'cd', and initialize it with 'ab'.
You didn't invoke AB's copy constructor, only a pointer's copy constructor,
so to say. Because 'cd' is not an AB -- it's a 'pointer to an AB'.
Now both 'ab' and 'cd' point to the same AB.

To invoke AB's copy constructor, you might have done this:


AB* ef = new AB(*ab);

This creates a new AB object, with its own memory, on the heap.
The new object is a copy of the object pointed to by 'ab' (if your copy
constructor does its job correctly).
The address of the new object is kept in 'ef'.

I couldn't say 'new AB(ab)' because 'ab' is a 'pointer to an AB', while
your copy constructor expects 'an AB', so I dereferenced 'ab' by saying
'*ab'.

HTH.

Ulrich Eckhardt

unread,
Nov 20, 2009, 4:49:42 AM11/20/09
to
AY wrote:
> I have a situation where I'm required to generate a copy of an object.
> I have simplified the class into a tiny one, but in reality its a huge
> class with multiple struct types as data member.

> Here is my simplified class:
>
> class AB
> {
> private:
> int a;
> public:
>
> AB(int ar): a(ar){}

Make this

explicit AB(int ar)...

The point is that if a function expects an AB and you just call it with 42
as argument, that will automatically create and later on destroy an AB.

> AB(const AB& arg)
> {
> a = arg.a;
> }

Prefer initialization lists:

AB(AB const& arg): a(arg.a) {}

> int main()
> {
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);

This is equivalent to

AB* cd = ab;

> My Qn is in the statement - AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?

No. Both 'ab' and 'cd' are distinct objects. Try this:

std::cout << "address of ab=" << static_cast<void*>(&ab) << std::endl;
std::cout << "address of cd=" << static_cast<void*>(&cd) << std::endl;

> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?

No, 'cd' is on the stack.

> What happens here exactly.

You are copying a pointer object. Note: In addition to the two lines above,
try this:

std::cout
<< "address stored in ab=" << static_cast<void*>(ab) << std::endl
<< "address stored in cd=" << static_cast<void*>(cd) << std::endl;

The point is that they are distinct objects but that they are both pointers
to the same AB object. In other words, you never copied the object and your
question about the copy ctor is not.


If you have a Java background, where everything is allocated with 'new', you
must learn not to repeat that in C++ where it is wrong and unhealthy. Please
read a good book on C++, it will clean up that confusion.

Uli

marcin...@gmail.com

unread,
Nov 20, 2009, 4:54:47 AM11/20/09
to
On 20 Lis, 00:04, AY <techreposit...@gmail.com> wrote:
> I have a situation where I'm required to generate a copy of an object.
> I have simplified the class into a tiny one, but in reality its a huge
> class with multiple struct types as data member.
>
> Here is my simplified class:
>
> class AB
> {
> private:
> int a;
> public:
>
> AB(int ar): a(ar){}
>
> AB(const AB& arg)
> {
> a = arg.a;
> }
>
> };
>
> int main()
> {
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);
>
> return 0;
>
> }
>
> My Qn is in the statement - AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?
>
> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?
>
> What happens here exactly.
>
> Thanks in advance friends,
> - AY
>
In your example cd is a copy of ab pointer, not object pointed by ab.
You call copy constructor the same way you call other constructors:

AB* cd = new AB(ab); // on heap

or

AB cd(ab); // on stack

Cheers
Sfider

Martin B.

unread,
Nov 20, 2009, 5:01:52 AM11/20/09
to
AY wrote:
> I have a situation where I'm required to generate a copy of an object.
> I have simplified the class into a tiny one, but in reality its a huge
> class with multiple struct types as data member.
>
> Here is my simplified class:
>
> class AB
> {
> private:
> int a;
> public:
>
> AB(int ar): a(ar){}
>
> AB(const AB& arg)
> {
> a = arg.a;
> }
> };

Better make this:
AB(const AB& arg)
: a(arg.a)
{ }

Also: if you define a copy-ctor you also want to define a copy operator.
(Or as in this case - just go with the copiler generated ones.)

>
> int main()
> {
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);
>
> return 0;
> }
>
>
> My Qn is in the statement - AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?
>
> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?
>

cd and ab are POINTERS to a an object constructed in free-store (heap)
and as such both point to exactly the same AB entitiy. Initializing /
assigning a pointer doesn't invoke anything.

AB* p = ab; // or p(ab); // pointer init: p and ab point to the same thing
AB obj(*ab); // or maybe obj = *ab; // invokation of copy-ctor and
creation of a new object (on the stack)

br,
Martin

red floyd

unread,
Nov 20, 2009, 5:01:51 AM11/20/09
to
On Nov 19, 3:04 pm, AY <techreposit...@gmail.com> wrote:
> I have a situation where I'm required to generate a copy of an object.
> I have simplified the class into a tiny one, but in reality its a huge
> class with multiple struct types as data member.
>
> Here is my simplified class:
>
> class AB
> {
> private:
> int a;
> public:
>
> AB(int ar): a(ar){}
>
> AB(const AB& arg)
> {
> a = arg.a;
> }
>
> };
>
> int main()
> {
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);
>
> return 0;
>
> }
>
> My Qn is in the statement - AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?
>
> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?
>
> What happens here exactly.
>

No constructor gets called, since the statement:

AB* cd(ab) is exactly the same as AB* cd = ab;

You are using Java-isms. You need to go back and study your textbook.

Alp Mestan

unread,
Nov 20, 2009, 5:01:51 AM11/20/09
to
On Nov 20, 12:04 am, AY <techreposit...@gmail.com> wrote:
> Does object 'cd' uses the same memory as object 'ab' ?
>
> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not
> think, maybe I'm wrong ] ?
>
> What happens here exactly.

The 'cd' pointer will point to the same memory place as ab does. Just
try to output 'ab' and 'cd' and you'll figure it out.

What happens is that :
1/ you create the 'ab' pointer and it'll call AB::AB(int)
2/ you create the 'cd' pointer, which will be initialized with 'ab',
that is you create a pointer that'll be initialized with 'ab's
"value".

It's the very same as writing :
AB* cd = ab;

Francis Glassborow

unread,
Nov 20, 2009, 4:06:47 PM11/20/09
to
John H. wrote:
<excellent presentation snipped>

> Note you are copying the pointer, not the actual AB object. Only one
> AB object exists, with two way to access it. Your copy constructor is
> never called. To invoke the copy constructor you could do something
> like:
> AB* ab = new AB(7);
> AB* cd = new AB(ab);
But that line should read

AB* cd = new AB(*ab);

It is the object pointed to that you want to pass to the AB copy ctor.
Of course the designer of AB might have included a ctor that takes a
pointer to AB but that is unlikely.

Daya S. Prasad

unread,
Nov 20, 2009, 4:05:01 PM11/20/09
to
> AB* ab = new AB(7);
>
> //AB* cd = new AB;
>
> AB* cd(ab);
>
> Does object 'cd' uses the same memory as object 'ab' ?
>
> Is any memory allocated in the heap for 'cd' like 'ab' [ I do not think, maybe I'm wrong ] ?

I'm little bit confuse what you want to know. But anyway...
Well definitely, 'ab' and 'cd' both pointer will have different memory
in heap but object pointed out by both pointer is same means same
object.

--
Daya S. Prasad

Taras Shevchuk

unread,
Nov 20, 2009, 4:09:57 PM11/20/09
to
John H.:

> AB object exists, with two way to access it. Your copy constructor is
> never called. To invoke the copy constructor you could do something
> like:
> AB* ab = new AB(7);
> AB* cd = new AB(ab);
> Now we have:
> - An AB object on the heap
> - A pointer that is a local variable called ab, which points to the
> above mentioned AB object
> - A second AB object on the heap that is a copy of the first AB object
> - A pointer that is a local variable called cd, which points to the
> second AB object.

Small note:

> AB* cd = new AB(*ab);

ab is pointer. We should dereference it before putting to copy
constructor or class AB should have constructor like this:
class AB
{
...
AB(AB* ab)
{
...
}
...
};

red floyd

unread,
Nov 21, 2009, 11:50:49 PM11/21/09
to
On Nov 20, 1:05 pm, "Daya S. Prasad" <mail....@gmail.com> wrote:
> > AB* ab = new AB(7);
>
> > //AB* cd = new AB;
>
> > AB* cd(ab);
>
> > Does object 'cd' uses the same memory as object 'ab' ?
>
> > Is any memory allocated in the heap for 'cd' like 'ab' [ I do not think, maybe I'm wrong ] ?
>
> I'm little bit confuse what you want to know. But anyway...
> Well definitely, 'ab' and 'cd' both pointer will have different memory
> in heap but object pointed out by both pointer is same means same
> object.
>

No, only a single heap allocation will occur, as written.


--

0 new messages