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

reference vs pointer

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
9 2962
"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

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

<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 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...
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;
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.