Pointer question

B

BCC

Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?

p2 = p1;
p1 = new CMyClass();

and have both pointers point to the same object?

I don't think so, but thought I'd check for sure.

Thanks,
Bryan
 
J

John Carson

BCC said:
Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?

p2 = p1;
p1 = new CMyClass();

and have both pointers point to the same object?

I don't think so, but thought I'd check for sure.

Thanks,
Bryan

You need to use a reference, which is a kind of alias.

CMyClass* p1;

// makes p2 a reference to a pointer and initialises it to
// be an reference to the pointer p1
CMyClass* &p2 = p1;

p1 = new CMyClass();
 
D

David White

BCC said:
Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?

p2 = p1;
p1 = new CMyClass();

and have both pointers point to the same object?

Unless your computer runs on tachyons, then no.

If p1 has not been initialized before the first line, then there is an
extremely remote possibility that it will accidentally be the same address
as that returned by the "new" expression.

DW
 
J

Josephine Schafer

BCC said:
Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();

No need of the braces.
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?
No.

p2 = p1;

Copy the junk address p1 is holding into p2.
p1 = new CMyClass();

p1 now holds a valid heap address while p2 still holds the junk address.
and have both pointers point to the same object?

You see the difference now?
 
D

David White

David White said:
If p1 has not been initialized before the first line, then there is an
extremely remote possibility that it will accidentally be the same address
as that returned by the "new" expression.

Or this might do it on some compilers:
p1 = new CMyClass;
delete p1;
// now your code
p2 = p1;
p1 = new CMyClass();

DW
 
J

Jakob Bieling

David White said:
Or this might do it on some compilers:
p1 = new CMyClass;
delete p1;
// now your code
p2 = p1;
p1 = new CMyClass();


Oi, and now you expect p1 to point to the same object as p2? Now the
possibility that this works is jus as remote as to assume that an
uninitialized p1 'accidently' points to p2. Actually, this code is even
invalid (as I have learned a few days ago), because you are assigning p1 to
p2.

regards
 
J

Jakob Bieling

BCC said:
Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?

p2 = p1;
p1 = new CMyClass();

and have both pointers point to the same object?

I don't think so, but thought I'd check for sure.


As you have it, no, like Josephine and David pointed out already. But
you can change the code a little, so that p1 always points to the same thing
p2 points to:

CMyClass* p2;
CMyClass*& p1 = p2; // make p1 a reference to a pointer

p2 = new CMyClass ();
// now p1 and p2 point to the same object

hth
 
D

Default User

Jakob said:
Oi, and now you expect p1 to point to the same object as p2? Now the
possibility that this works is jus as remote as to assume that an
uninitialized p1 'accidently' points to p2. Actually, this code is even
invalid (as I have learned a few days ago), because you are assigning p1 to
p2.


It's not THAT ridiculous. Some people don't understand that
initializations store values, not operations. I've seen questions that
indicate people thought:


int a;
int b;
int c = a + b;


Would always set c to be the value of a + b. So if a or b changed, c
would automagically do so as well. It's not the way things work of
course.




Brian Rodenborn
 
D

David White

Jakob Bieling said:
Oi, and now you expect p1 to point to the same object as p2?

No, I said, "Or this might do it on some compilers", which establishes the
entire post as implementation-dependent. All you need is for the second
"new" to return the same address as the first, which it might well do
because the first has been deleted. Regarding the "p2 = p1; ", I don't know
if it's invalid according to the standard, but, let's face it, is there a
compiler on Earth that wouldn't simply take the bit pattern in p1 and stick
it in p2? (again, I said "some compilers").
Now the
possibility that this works is jus as remote as to assume that an
uninitialized p1 'accidently' points to p2.

I don't think so.

DW
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top