473,783 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2682
al*****@gmail.c om 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.c om wrote:
al*****@gmail.c om 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.c om wrote:
in*****@gmail.c om wrote:
al*****@gmail.c om 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.goo glegroups.com.. .
|
| in*****@gmail.c om wrote:
| > al*****@gmail.c om 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.goo glegroups.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.c om wrote:
in*****@gmail.c om wrote:
al*****@gmail.c om 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

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

Similar topics

110
9961
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 must be an object instead of
9
3002
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 tell me which one will be more efficient and why? as far as i know both must be same, because i read somewhere that internally
18
3539
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
5403
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
51
4475
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
2402
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
11
4241
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
29
3653
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
41
3687
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 pointer and reference are and how they relate to each other.
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
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
7494
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
6737
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
5379
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
4044
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.