473,388 Members | 1,322 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,388 software developers and data experts.

reference vs. pointer

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

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

<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
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
int& = *new int;


Typo:
int& i = *new int;
-Tomás
Apr 1 '06 #7
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
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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
11
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.