Reference Variable Vs Pointer

A

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)?
 
K

Kai-Uwe Bux

AB said:
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
 
R

Roland Pibinger

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

Ron Natalie

Roland said:
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 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top