473,811 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2920
BCC <a@b.c> wrote in message
news:wI******** *************@n ewssvr21.news.p rodigy.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******** *************@n ewssvr21.news.p rodigy.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.provi ded> 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.provi ded> wrote in message
news:Qf******** *********@nasal .pacific.net.au ...
David White <no@email.provi ded> 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******** *************@n ewssvr21.news.p rodigy.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******** *************@n ewssvr21.news.p rodigy.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******** *************@n ewssvr21.news.p rodigy.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.ne t> wrote in message
news:bf******** *****@news.t-online.com...
"David White" <no@email.provi ded> wrote in message
news:Qf******** *********@nasal .pacific.net.au ...
David White <no@email.provi ded> 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
5034
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
20
6579
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 area of ram that's not reserved by the program. Accessing memory through such pointer is likely to result in core dump (memory access violation)"
4
3636
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
204
13142
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 = {0,1,2,4,9};
17
2726
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 compiler I use for the Samsung 16 bit Smartcard chips and I want to know if it is compliant with the ANSI standard or if it violates it. Have a look at this super simple example, where the value of b is incorrect:
26
3067
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
9
3208
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 always been under the impression that if I had a pointer variable, p which contained the member "first", I could access this member variable either by: (*p).first OR p->first . That is, in general I thought p->x was just shorthand for (*p).x ?
5
1666
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
3378
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 array of int */\ ap = &a1; printf("%d\n", **ap);
2
2499
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 attempt to dereference the null pointer. A better definition of the function would be: static void showtext(const char **ptr) { if (ptr == NULL) {
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.