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

reference vs. pointer

P: n/a
I get that these two are different

int* get()
{
static int m;
return &m;
}

int& get()
{
static int m;
return m;
}

int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?
}
but can there be a case when pointers may be confused with references?
The reason I am asking is because the pointer contains the address of
an object so its value is a reference.

Mar 31 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
al*****@gmail.com wrote:
int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?
It needs to be *p. Otherwise it will not compile.
}
but can there be a case when pointers may be confused with references?
Not really?
The reason I am asking is because the pointer contains the address of
an object so its value is a reference.


Huh?

Apr 1 '06 #2

P: n/a

in*****@gmail.com wrote:
al*****@gmail.com wrote:
int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?
It needs to be *p. Otherwise it will not compile.


thats true, but WHY?
}
but can there be a case when pointers may be confused with references?


Not really?
The reason I am asking is because the pointer contains the address of
an object so its value is a reference.


Huh?


Technically a reference denotes the address of an object, right? If
this address is stored in a pointer, why can't we use the pointer as a
reference?

Apr 1 '06 #3

P: n/a
al*****@gmail.com wrote:
in*****@gmail.com wrote:
al*****@gmail.com wrote:
int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?
It needs to be *p. Otherwise it will not compile.

thats true, but WHY?
Because they are different types.
Technically a reference denotes the address of an object, right? If
this address is stored in a pointer, why can't we use the pointer as a
reference?


Again, because they are different types ;) No really, just because
they serve a similar purpose and might work similar internally, does not
imply that you can use them interchangeably.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 1 '06 #4

P: n/a

<al*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
|
| in*****@gmail.com wrote:
| > al*****@gmail.com wrote:
| > > int& get()
| > > {
| > > static int m;
| > > int* p=&m;
| > > return p; // should it be *p or p?
| >
| > It needs to be *p. Otherwise it will not compile.
|
| thats true, but WHY?

Because the return type is a reference. A reference is a permanent alias
to a guarenteed-valid object, not a pointer to what *might* be an
object.

Whether i call you "Allen" or "al" makes no difference, i'm still
refering to you. Your nickname is not your mailing address. While
remembering the nickname does occupy space in my tiny brain, it beats
having to use a mailing address since i don't even know if a house is
found there.

That sounds dumb, doesn't it? Well thats about how dumb comparing a
pointer to a reference gets you.

|
| >
| > > }
| > > but can there be a case when pointers may be confused with
references?
| >
| > Not really?
| >
| > > The reason I am asking is because the pointer contains the address
of
| > > an object so its value is a reference.
| >
| > Huh?
|
| Technically a reference denotes the address of an object, right? If
| this address is stored in a pointer, why can't we use the pointer as a
| reference?
|

No, absolutely not. A pointer denotes an address, it does not
neccessarily point to an object (null pointer, invalid pointer).
Additionally, a pointer doesn't know if and when its pointing to
anything. In fact, the only truth a pointer knows is that it itself is
associated with a type. There is nothing dumber than a pointer.

Pointer == bugs
Pointer == evil
Dumb Ptrs were definitely created by the devil.

A reference is often implemented with a pointer but the compiler applies
special rules that convey it very unique and disireable characteristics.

a) a reference to an object is not allowed to refer to another object
(even of the same type). The relationship between the reference and the
target is quarenteed to remain unchanged without exceptions.

b) a reference must be initialized and must remain valid at all cost
(hence: extend an object's lifetime).

c) there is no such thing as a null reference (at least not in the sense
a pointer can be null). Creating or accesing a reference implies a valid
target. That too is guarenteed.

int& r; // illegal
int n;
int& r_n = n;
....
r_n = 4; // modifies n transparently since r_n *is* n
// the code in ... can't break that relationship

These characteristics mean that code which rely on references loose 90%
of the ugly, sordid and gratuitous bugs that dumb pointers bring to the
language.

Always, always prefer a reference whenever possible. Even when a dumb
pointer does the job perfectly, a reference is still a far, far better
choice.

I can safely state that most of the coders in here have an overwhelming,
itching obsession to convert each and every pointer they come accross
into a reference.

Some probably loose sleep over it. Actually, thats not really true.
Converting every single pointer that can be converted means that the
newly released code will not mysteriously cease to function the very day
the client starts using it.

To drive the point yet deeper: a reference is nothing less than a
runtime check. As an example:

void mutiply(double& a, double& b)
{
a *= b;
}

Except for min/max numerical limits in this case, its virtually
impossible to use that function by calling it with invalid/undefined
variables. The compiler would generate an error if it can't initialize
those 2 references. With dumb pointers, however, the runtime system is
utterly blind. In my opinion, a dumb pointer is assumed to be
null/invalid until proven otherwise. That disease cannot infect a
reference. So...

reference != pointer.

In fact, the only thing those 2 have in common is that they both occupy
memory space (and sometimes not the same amount of space).

Apr 1 '06 #5

P: n/a
posted:
I get that these two are different

int* get()
{
static int m;
return &m;
}

int& get()
{
static int m;
return m;
}

int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?
}
but can there be a case when pointers may be confused with references?
The reason I am asking is because the pointer contains the address of
an object so its value is a reference.


Don't think of "p" as a value. Think of it as an expression, which, when
evaluated, can yield a value. "p", as an expression, has a type.

In your last function above, look at the types. Firstly, we have:

static int m;
int* p = &m;

"m" is an expression of the type, "int". Therefore, "&m" is an expression
of the type "int*". (Don't be thinking about values yet). "p" on the left
hand side is also of the type "int*", so everything is fine and dandy
with our assignment statement. The types match, so now we can evaluate
"&m" and store the value in "p".

Your function's return type is "int&". Therefore, when you make the
"return" statement, the type of the expression after the "return" keyword
must be "int", and, not only that, it must be an "l-value" so that you
can bind a reference to it. (It also must be non-const in this case.).
The following won't work:

return 5;

because, while the type requirements have been satisfied, the "l-value"
and "const" requirements haven't.

As I said, "p" is an expression of the type "int*". We want this function
to return a reference to an object of the type "int". We turn an "int*"
into an "int" by using the asterisk operator like as follows: *p.

Therefore, the "return" statement should be:

return *p;
The moral of the story is, that I find that I can understand these things
if I think in terms of expressions rather than values. Think of "p" as an
expression of type "int*"; therefore if we put an asterisk before it
like:

*p

then we have an expression of type "int". If we put an ampersand before
it:

&p

then we have an expression of type "int**" (not taking into account the
constness).
If you get your head around the following line of code I'm about to
write, you'll have a firm understanding of how it all works:

int& = *new int;
-Tomás
-Tomás
Apr 1 '06 #6

P: n/a
int& = *new int;


Typo:
int& i = *new int;
-Tomás
Apr 1 '06 #7

P: n/a
Tomás wrote:
int& = *new int;


Typo:
int& i = *new int;
-Tomás


Interesting. Let me make an attempt to see what the code does.
i is of type int&
*new int = *(new int) = *(pointer to memory allocated to store an
integer) = **(memory allocated for int)

thus if i=*new int; then i should be of type **?

Apr 1 '06 #8

P: n/a
Peter_Julian wrote:
<al*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
|
In fact, the only thing those 2 have in common is that they both occupy
memory space (and sometimes not the same amount of space).


So references do occupy memory? I thought since they are aliases they
don't. In fact from what I have learnt the difference between a
reference and a pointer is that the latter is a variable (usually auto
variable), and a reference isn't.

Apr 1 '06 #9

P: n/a

Jakob Bieling wrote:
al*****@gmail.com wrote:
in*****@gmail.com wrote:
al*****@gmail.com wrote:
int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?

It needs to be *p. Otherwise it will not compile.

thats true, but WHY?


Because they are different types.


Is there a difference between a C reference and a C++ reference? By C
reference I mean
int& a;
type declarations.

Apr 1 '06 #10

P: n/a

al*****@gmail.com wrote:
Tomás wrote:
int& = *new int;


Typo:
int& i = *new int;
-Tomás


Interesting. Let me make an attempt to see what the code does.
i is of type int&
*new int = *(new int) = *(pointer to memory allocated to store an
integer) = **(memory allocated for int)

thus if i=*new int; then i should be of type **?


No, you've gone the wrong way in your last step.

*new int => *(new int)
*(new int) => *(pointer to an int)

Now what happens when you apply * the dereference operator to a
pointer? You get the object pointed to by that pointer, so

*(poiner to an int) => (the int)

So in the code

int& i = *new int;

i is a reference to the newly created int.

Gavin Deane

Apr 1 '06 #11

P: n/a
Gavin Deane wrote:
al*****@gmail.com wrote:
Tomás wrote:
> int& = *new int;

Typo:
int& i = *new int;
-Tomás


Interesting. Let me make an attempt to see what the code does.
i is of type int&
*new int = *(new int) = *(pointer to memory allocated to store an
integer) = **(memory allocated for int)

thus if i=*new int; then i should be of type **?


No, you've gone the wrong way in your last step.

*new int => *(new int)
*(new int) => *(pointer to an int)

Now what happens when you apply * the dereference operator to a
pointer? You get the object pointed to by that pointer, so

*(poiner to an int) => (the int)

So in the code

int& i = *new int;

i is a reference to the newly created int.

Gavin Deane


Ah yes, you are right. Blunder! And to think I'm going to work at a
financial company this summer!

Apr 1 '06 #12

P: n/a
al*****@gmail.com wrote:
Jakob Bieling wrote:
al*****@gmail.com wrote:
in*****@gmail.com wrote:
al*****@gmail.com wrote:
> int& get()
> {
> static int m;
> int* p=&m;
> return p; // should it be *p or p?

It needs to be *p. Otherwise it will not compile.

thats true, but WHY?


Because they are different types.


Is there a difference between a C reference and a C++ reference? By C
reference I mean
int& a;
type declarations.


As far as I know, C does not have any references .. ?
--
jb

(reply address in rot13, unscramble first)
Apr 1 '06 #13

P: n/a
No, you've gone the wrong way in your last step.

*new int => *(new int)
*(new int) => *(pointer to an int)

Now what happens when you apply * the dereference operator to a
pointer? You get the object pointed to by that pointer, so

*(poiner to an int) => (the int)

So in the code

int& i = *new int;

i is a reference to the newly created int.

Gavin Deane


Here's how I like to think of it.

int &i = *new int;

First of all, that's the same as:

int &i = * (new int);

The thing on the left hand side of the assignment is of the type "int". The
thing on the left hand side of the assignment must an l-value of type "int".
Let's break it down into expressions. First let's look at the following
expression:

new int

The type of this expression is "int *", as "new" returns a pointer to the
object that's created. If you stick an asterisk behind an expression of type
"int*", you're left with an expression of type "int". Therefore, the
following expression is of the type "int":

*new int

Now we can bind a reference to it.

int &i = *new int;

delete &i;

-Tomás
Apr 1 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.