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

reference vs pointer

P: n/a
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
references are implemented as "constant pointers"
Aug 28 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Sandy" <a@a.com> wrote in message news:de********@netnews.net.lucent.com...
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
references are implemented as "constant pointers"


Performance (and underlying implementation) is typically the same,
although an optimizing compiler might in some cases generate faster code
for the reference parameter, because it knows the address is not NULL.

So when should either option be used for passing parameters?
Unless it is desirable to be able to provide NULL as a parameter,
references ought to be preferred in C++ programs.
However, I have seen coding style guides that mandated the
use of pointers to output variables, for readability reasons:
processData( src1, src2, &dst ); // highlights 'dst' as an output
This is not my preferred approach.
Hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Aug 28 '05 #2

P: n/a

"Sandy" <a@a.com> wrote in message news:de********@netnews.net.lucent.com...
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?


Apart from what Ivan has already said, you may find the following link
helpful:
http://www.parashift.com/c++-faq-lite/references.html

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Aug 28 '05 #3

P: n/a
There is 1 difference b/w a pointer and a reference.
Pointer can point to a NULL value.
When a pointer is incremented or decremented, it will point to the next
or previous value relative to its base type.

Reference cannot point to NULL.
Since reference is an implicit pointer & a another name for an object
or variable, incrementing or decrementing the reference will just
operate on the value.
For independent references, an initialization has to be done.

Reference Or Pointer Is Efficient...Depends on the application.
Because, I think for data structures, only pointers will be more
useful, than references.

Any other opinion is appreciated.

AV.

Sandy wrote:
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
references are implemented as "constant pointers"


Aug 28 '05 #4

P: n/a
<am*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
There is 1 difference b/w a pointer and a reference.
There is more than one.
Pointer can point to a NULL value.
What it points to has nothing to do with the difference.

The difference is that a pointer need not have a valid
value, and a reference must always refer to an object
(i.e. a reference must be initialized, a pointer need not be).
When a pointer is incremented or decremented, it will point to the next
or previous value relative to its base type.
As long as that 'next' (address) value lies within the boundaries
of the same object, or one element past the end of an array.

Reference cannot point to NULL.
Of course not. References do not 'point'. A reference is not a pointer.
Since reference is an implicit pointer
A reference is not any kind of pointer.
& a another name for an object
Yes, a reference is another name (an 'alias') for an existing object.
or variable, incrementing or decrementing the reference will just
operate on the value.
Yes, any operations applied to the name of a reference will
be applied to the object to which it refers.
For independent references,
A reference is a reference. There's nothing called
'independent reference' defined by C++.
an initialization has to be done.
*All* references must be initialized.
(At first glance, it might seem that a function's reference
parameter is not initalized -- but it is -- each time the
function is invoked.)
Reference Or Pointer Is Efficient...Depends on the application.


It will mostly depend upon the implementation.

-Mike
Aug 28 '05 #5

P: n/a
> There is 1 difference b/w a pointer and a reference.
There is more than one.
what are the other differences b/w pointer and references ?

There's nothing called 'independent reference' defined by C++.
there are "independent references" in c++. for eg.

{
int a;
int &ref=a;
}

independent references are not commonly used in C++ as it causes more
confusion 'cause "ref" is just another name for 'a'.
and moreover references can also point to a constant variable....for
eg.&ref=9;
for more on "independent references", refer "the complete reference
c++, by herbet schildt"

A reference is not any kind of pointer.
"reference is an implicit pointer".
if it is not a pointer, it cannot point to the same location as where
the object or variable or function argument resides in memory.
again, if it is not a pointer, a copy of the variable, object or
function argument will be created.

Reference Or Pointer Is Efficient..........It will mostly depend upon the implementation...


well, i dont understand this...'cause can references be used in data
structures instead of pointers...can u explain.

Aug 29 '05 #6

P: n/a
am*****@gmail.com wrote:
There is 1 difference b/w a pointer and a reference.
There is more than one.

what are the other differences b/w pointer and references ?


This was covered recently - post is here:
http://groups.google.com/group/comp....6ace77d2529b48
Aug 29 '05 #7

P: n/a
we have seen the difference b/w references & pointers, but which is
more efficient, pointers or references ?
again, can references be used in data structures ?
Gianni Mariani wrote:
am*****@gmail.com wrote:
There is 1 difference b/w a pointer and a reference.
There is more than one.

what are the other differences b/w pointer and references ?


This was covered recently - post is here:
http://groups.google.com/group/comp....6ace77d2529b48


Aug 29 '05 #8

P: n/a
am*****@gmail.com wrote:
we have seen the difference b/w references & pointers, but which is
more efficient, pointers or references ?
This was answered earlier. It's implementation dependant. In essance a
good optimizing compiler may make use of knowledge that a reference
can't be reseated, however it may also make the same kinds of
optimizations using const pointers. In essance, it's a useless question.
again, can references be used in data structures ?
Yes, I do it all the time - as long as you initialize the reference in
the constructor.


Gianni Mariani wrote:
am*****@gmail.com wrote:
There is 1 difference b/w a pointer and a reference.
There is more than one.
what are the other differences b/w pointer and references ?


This was covered recently - post is here:
http://groups.google.com/group/comp....6ace77d2529b48


Aug 29 '05 #9

P: n/a

<am*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
There is 1 difference b/w a pointer and a reference.
There is more than one.
what are the other differences b/w pointer and references ?


See the link posted by Gianni.

There's nothing called 'independent reference' defined by C++.
there are "independent references" in c++. for eg.

{
int a;
int &ref=a;
}


In your example, the name 'ref' denotes a reference. Period.
It has no special attributes such as 'independent'

independent references are not commonly used in C++ as it causes more
confusion 'cause "ref" is just another name for 'a'.
References only confuse those who don't understand what they
are and what they're for. Of course 'ref' is just another name
for 'a'. That's what a reference is.
and moreover references can also point to a constant variable....
References do *not* point. Repeat to yourself until it sinks in:
A reference is not a pointer. A reference is not a pointer ...
for
eg.&ref=9;
That's a syntax error. It has no meaning.
for more on "independent references", refer "the complete reference
c++, by herbet schildt"
Aha, that explains much. Herb's excellent writing style has duped
you (you're not alone in this) into believing his work is technically
correct (it's not). For selecting good quality C++ texts, consult
the peer reviews at www.accu.org


A reference is not any kind of pointer.
"reference is an implicit pointer".


No. A reference is not a pointer. Period. My source is the
official ISO standard for C++, not an incorrect textbook.

if it is not a pointer, it cannot point to the same location as where
the object or variable or function argument resides in memory.
It doesn't need to. It's not a pointer.
again, if it is not a pointer, a copy of the variable, object or
function argument will be created.
No, a reference does *not* cause a copy to be created. As a matter
of fact, this is one of the major benefits of using a reference.

Reference Or Pointer Is Efficient..........It will mostly depend upon the
implementation...


well, i dont understand this...'cause can references be used in data
structures instead of pointers...can u explain.


Yes, a class or struct member can be a reference (but imo has
limited practical value).

-Mike
Aug 30 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.