473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Pointer question

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
Jul 19 '05 #1
9 2838
BCC <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig 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

Jul 19 '05 #2

"BCC" <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig 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

Jul 19 '05 #3
David White <no@email.provided> wrote in message
news:w5*****************@nasal.pacific.net.au...
BCC <a@b.c> 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

Jul 19 '05 #4
"David White" <no@email.provided> wrote in message
news:Qf*****************@nasal.pacific.net.au...
David White <no@email.provided> wrote in message
news:w5*****************@nasal.pacific.net.au...
BCC <a@b.c> 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)
Jul 19 '05 #5
"BCC" <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig 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)
Jul 19 '05 #6
"BCC" <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig 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)

Jul 19 '05 #7
"Josephine Schafer" <js*@usa.net> wrote in message
news:bf************@ID-192448.news.uni-berlin.de...

"BCC" <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig 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? ;)
Jul 19 '05 #8


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
Jul 19 '05 #9
Jakob Bieling <ne*****@gmy.net> wrote in message
news:bf*************@news.t-online.com...
"David White" <no@email.provided> wrote in message
news:Qf*****************@nasal.pacific.net.au...
David White <no@email.provided> wrote in message
news:w5*****************@nasal.pacific.net.au...
BCC <a@b.c> 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

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.