Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer question

Reply
Thread Tools

Pointer question

 
 
BCC
Guest
Posts: n/a
 
      07-22-2003
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


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      07-22-2003
"BCC" <(E-Mail Removed)> wrote in message
news:wI3Ta.2868$(E-Mail Removed) y.com
> 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();


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      07-22-2003
BCC <(E-Mail Removed)> wrote in message
news:wI3Ta.2868$(E-Mail Removed) y.com...
> 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



 
Reply With Quote
 
Josephine Schafer
Guest
Posts: n/a
 
      07-22-2003

"BCC" <(E-Mail Removed)> wrote in message
news:wI3Ta.2868$(E-Mail Removed) y.com...
> 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?

--
With best wishes,
J.Schafer



 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      07-22-2003
David White <(E-Mail Removed)> wrote in message
news:w54Ta.1062$(E-Mail Removed)...
> BCC <(E-Mail Removed)> wrote in message
> 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



 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      07-22-2003
"David White" <(E-Mail Removed)> wrote in message
news:Qf4Ta.1063$(E-Mail Removed)...
> David White <(E-Mail Removed)> wrote in message
> news:w54Ta.1062$(E-Mail Removed)...
> > BCC <(E-Mail Removed)> wrote in message
> > 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();



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
--
jb

(replace y with x if you want to reply by e-mail)


 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      07-22-2003
"BCC" <(E-Mail Removed)> wrote in message
news:wI3Ta.2868$(E-Mail Removed) y.com...
> 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
--
jb

(replace y with x if you want to reply by e-mail)


 
Reply With Quote
 
John Ericson
Guest
Posts: n/a
 
      07-22-2003
"Josephine Schafer" <(E-Mail Removed)> wrote in message
news:bfiitc$f9sbe$(E-Mail Removed)-berlin.de...
>
> "BCC" <(E-Mail Removed)> wrote in message
> news:wI3Ta.2868$(E-Mail Removed) y.com...
> > Hi,
> >
> > If I have something like this:
> > CMyClass* p1;
> > CMyClass* p2;
> >
> > I know I can do:
> > p1 = new CMyClass();

>
> No need of the braces.
>


Perhaps the OP is initializing a POD?


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      07-22-2003


Jakob Bieling wrote:

> 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
 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      07-22-2003
Jakob Bieling <(E-Mail Removed)> wrote in message
news:bfj1hh$v4u$03$(E-Mail Removed)-online.com...
> "David White" <(E-Mail Removed)> wrote in message
> news:Qf4Ta.1063$(E-Mail Removed)...
> > David White <(E-Mail Removed)> wrote in message
> > news:w54Ta.1062$(E-Mail Removed)...
> > > BCC <(E-Mail Removed)> wrote in message
> > > 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();

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments