By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

Expecting two different pointers, yet obtaining only one

P: n/a
I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?

A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
The Cool Giraffe wrote:
I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?
Well obviously your impression of how & works is wrong.
>
A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?
Impossible, there is only one bops variable, so it only has one address,
so all pointers to it will be the same.

Why do you want two different pointers? I have the feeling that your
groping towards the concept of dynamic memory allocation. If so you need
to look up the new and delete operators, they will give you different
pointers.

void* ptr1 = new int;
void* ptr2 = new int;

std::cout << ptr1 << std::endl;
std::cout << ptr2 << std::endl;

john
Feb 14 '07 #2

P: n/a
The Cool Giraffe wrote:
I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?
Because the object bops only has one address, and you stored that address in
both pointer variables.
A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?
You already succeeded. You are just misinterpreting the output of you
program. You created three different objects in this snippet:

a) an object of type int, called bops
b) an object of type void*, called gPtr_1
c) an object ot type void*, called gPtr_2

What your output shows is that the two objects of type void* have the same
_value_. They are two _different_ pointers with the same value, and this
value is the address of the first object bops.
Best

Kai-Uwe Bux
Feb 14 '07 #3

P: n/a
John Harrison wrote/skrev/kaita/popisal/schreibt :
The Cool Giraffe wrote:
>I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?

Well obviously your impression of how & works is wrong.
Well, i haven't really claimed otherwise. :)
Perhaps i should make it sound _LESS_ confident. I don't
claim in any way i'm competent in the area of C++. Sorry.
>A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?

Impossible, there is only one bops variable, so it only has one address,
so all pointers to it will be the same.

Why do you want two different pointers?
To laborate with the language. It less of "i want to do XXX"
and more of "what happens if i do XXX".
... you need to look up the new and delete operators, they will give you
different pointers.

void* ptr1 = new int;
void* ptr2 = new int;

std::cout << ptr1 << std::endl;
std::cout << ptr2 << std::endl;

The code you showed above creates a pair of different
pointer, all right, but i'd like them to point to the same
location (namely, the integer bops). Is it possible?

I was under the impression (potentially a wrong one)
that i could have two _different_ pointer with two
_different_ addresses still pointing to the same spot.
Is that wrong?

If it is wrong, that would mean that the first pointer
would be merely a reference to (of?) the other. Right?

Also, i'd like to thank you for the clear answer. :)

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 14 '07 #4

P: n/a
Kai-Uwe Bux wrote/skrev/kaita/popisal/schreibt :
The Cool Giraffe wrote:
>I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?

Because the object bops only has one address, and you stored that
address in both pointer variables.
>A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?

You already succeeded. You are just misinterpreting the output of you
program. You created three different objects in this snippet:

a) an object of type int, called bops
b) an object of type void*, called gPtr_1
c) an object ot type void*, called gPtr_2

What your output shows is that the two objects of type void* have the
same _value_. They are two _different_ pointers with the same value,
and this value is the address of the first object bops.

Got it. Thanks for the clarification.

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 14 '07 #5

P: n/a
On 14 feb, 09:10, "The Cool Giraffe" <giraf...@viltersten.comwrote:
I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?

A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)

You've already got 2 pointers to the bops-variable. The pointers own
address is different, which you can see by doing this;

std::cout << &gPtr_1 << std::endl;
std::cout << &gPtr_2 << std::endl;

But you get the same value when you send gPtr_1 and gPtr_2 to cout,
since you will see the address of bops.

Ps. Why don't you use int* instead of void* ??

Feb 14 '07 #6

P: n/a
jamx wrote/skrev/kaita/popisal/schreibt :
On 14 feb, 09:10, "The Cool Giraffe" <giraf...@viltersten.comwrote:
>I'm playing around with pointers trying to figure out how
the operator & works. My impression was that it can be
used to create a pointer to a thingy. So, i created the
following code and ran it.

int bops = 17;
void* gPtr_1 = & bops;
void* gPtr_2 = & bops;

std::cout << gPtr_1 << std::endl;
std::cout << gPtr_2 << std::endl;

I was expecting to see two different addresses but that
didn't happen. Why?

A follow up question - how can i create two _DIFFERENT_
pointers to the bops-variable?

You've already got 2 pointers to the bops-variable. The pointers own
address is different, which you can see by doing this;

std::cout << &gPtr_1 << std::endl;
std::cout << &gPtr_2 << std::endl;

But you get the same value when you send gPtr_1 and gPtr_2 to cout,
since you will see the address of bops.
Great. Thanks to you as well. Now i get it.
Ps. Why don't you use int* instead of void* ??
I wanted to go general and see if it works.
--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 14 '07 #7

P: n/a
>
>
The code you showed above creates a pair of different
pointer, all right, but i'd like them to point to the same
location (namely, the integer bops). Is it possible?

I was under the impression (potentially a wrong one)
that i could have two _different_ pointer with two
_different_ addresses still pointing to the same spot.
Is that wrong?
It depends what address you are talking about. All variables have
addresses. In addition pointer variables also have an address as their
value. You need to keep these two different concepts seperate.

int i = 1;
int* p1 = &i;
int* p2 = &i;

// these are both true
p1 == p2
&p1 != &p2

p1 and p2 are two different pointer variables, they have different
addresses, because &p1 and &p2 are different, BUT they both have the
address of i as their value, and that address is the same whatever the
pointer.
>
If it is wrong, that would mean that the first pointer
would be merely a reference to (of?) the other. Right?
No, not right, now your just confusing me (and yourself). Pointers are
pointers, they are not references. There's no magic here, it simpler
than you think, but you haven't got it yet. Unforutnately I'm not quite
sure what your error is, but I would guess that you are confusing the
fact that a pointer has an address as its value, but also has an address
of itself (like any other variable).

Also, i'd like to thank you for the clear answer. :)
Feb 14 '07 #8

P: n/a
John Harrison wrote/skrev/kaita/popisal/schreibt :
>The code you showed above creates a pair of different
pointer, all right, but i'd like them to point to the same
location (namely, the integer bops). Is it possible?

I was under the impression (potentially a wrong one)
that i could have two _different_ pointer with two
_different_ addresses still pointing to the same spot.
Is that wrong?

It depends what address you are talking about. All variables have
addresses. In addition pointer variables also have an address as their
value. You need to keep these two different concepts seperate.

int i = 1;
int* p1 = &i;
int* p2 = &i;

// these are both true
p1 == p2
&p1 != &p2
This was a very clear and educative example.
p1 and p2 are two different pointer variables, they have different
addresses, because &p1 and &p2 are different, BUT they both have the
address of i as their value, and that address is the same whatever the
pointer.
>>
If it is wrong, that would mean that the first pointer
would be merely a reference to (of?) the other. Right?

No, not right, now your just confusing me (and yourself). Pointers are
pointers, they are not references. There's no magic here, it simpler
than you think, but you haven't got it yet. Unforutnately I'm not
quite sure what your error is, but I would guess that you are
confusing the fact that a pointer has an address as its value, but
also has an address of itself (like any other variable).

Yes, this _WAS_ in deed what i got confused by. I've read
that somewhere but simply failed to grasp the idea anyway.
I'm very thankful and i really do appreciate the help.

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 15 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.