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

Reference Variable Vs Pointer

AB
Hello All

Suppose I've got two functions declared thus..

void func(const Type& val) ;

and

void func(const Type* val) ;

In terms of ease (read speed) in data retrieval which method is more
expensive(if at all so)?

Aug 6 '06 #1
4 1379
AB wrote:
Hello All

Suppose I've got two functions declared thus..

void func(const Type& val) ;

and

void func(const Type* val) ;

In terms of ease (read speed) in data retrieval which method is more
expensive(if at all so)?
You will have to measure yourself. However, the best guess to make without
measuring is that there will be no difference in performance whatsoever:
your compiler might implement references as pointers anyway. However,
references make some guarantees that pointers do not make whence the
compiler might be able to optimize the reference version better. Then
again, such optimizations might be buggy in your compiler and yield actual
pessimizations. So you will just have to measure. It could depend on
compiler, context, function body, ...

Also, you should first base your decision of whether to use pass-by-pointer
or pass-by-reference not on performance but on code-readability: which
version does communicate intent?
Best

Kai-Uwe Bux
Aug 6 '06 #2
On 5 Aug 2006 23:09:39 -0700, "AB" <ab*****@gmail.comwrote:
>Suppose I've got two functions declared thus..

void func(const Type& val) ;

and

void func(const Type* val) ;

In terms of ease (read speed) in data retrieval which method is more
expensive(if at all so)?
The second since you must check for the pointer for NULL inside func.

Aug 6 '06 #3
Roland Pibinger wrote:
On 5 Aug 2006 23:09:39 -0700, "AB" <ab*****@gmail.comwrote:
>Suppose I've got two functions declared thus..

void func(const Type& val) ;

and

void func(const Type* val) ;

In terms of ease (read speed) in data retrieval which method is more
expensive(if at all so)?

The second since you must check for the pointer for NULL inside func.
Unless you know that val will not be a null pointer. The standard
library makes this assumption all over the place.

Can't make any blanket comments about efficiency without knowing
what's inside the func .
Aug 6 '06 #4
AB
Thanks for your replies everyone.

Aug 7 '06 #5

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...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
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???????
7
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
3
by: mike7411 | last post by:
I was just wondering what typically happens under the hood when you pass a C++ variable by reference. It looks like a pointer gets passed in, but the compiler let's you access it as if it wasn't a...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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: 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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.