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

reference VS. pointer

I originally thought that using a reference to access an object is
more efficient than using a pointer.

Given an simple example like this,

int i = 12;
int& r = i;
int* p = &i;

the memory lay-out would look like,

|................ |
address of i and r |12 |
|.................|
|.................|
address of p |address of i |
|.................|

In order to access the value of i (12) through pointer p, you would have to
1. get the value of variable p;
2. use the value of p as the address of a memory chunk;
3. get the value of that memory address.

This would take at least two cpu clock ticks.

If reference r is used to access the value of i (12), you would only need to
1. get the value of variable r, since r is the same as i.

Surprisingly, when I wrote a short program to verify this, I got
totally reversed result. The pointer is faster than reference!
My test is based on GCC 3.2 on a W2K system.

Replies are appreciated.

-calvin
Jul 22 '05 #1
3 2049

"calvin" <ca******@operamail.com> wrote in message news:38**************************@posting.google.c om...
int i = 12;
int& r = i;
int* p = &i;

the memory lay-out would look like,
Not necessarily.


Surprisingly, when I wrote a short program to verify this, I got
totally reversed result. The pointer is faster than reference!
My test is based on GCC 3.2 on a W2K system.

Replies are appreciated.


Lets see your test program. Also did you turn on the optimizer?
There is no point in benchmarking most compiler performances in the
unoptimized mode.
Jul 22 '05 #2
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f**********************@news.newshosting.com ...

"calvin" <ca******@operamail.com> wrote in message news:38**************************@posting.google.c om...
int i = 12;
int& r = i;
int* p = &i;

the memory lay-out would look like,


Not necessarily.

Surprisingly, when I wrote a short program to verify this, I got
totally reversed result. The pointer is faster than reference!
My test is based on GCC 3.2 on a W2K system.

Replies are appreciated.


Lets see your test program. Also did you turn on the optimizer?
There is no point in benchmarking most compiler performances in the
unoptimized mode.


Also when the optimizer is on, there is big chance it doesn't matter whether
you use pointers or references. A good exercise is to study the assembly
output of an optimizing compiler; one of the most important lessons one will
learn is that that those nano optimizations are futile; in many cases they
have no effect whatsoever.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #3
Peter van Merkerk wrote:


Also when the optimizer is on, there is big chance it doesn't matter whether
you use pointers or references. A good exercise is to study the assembly
output of an optimizing compiler; one of the most important lessons one will
learn is that that those nano optimizations are futile; in many cases they
have no effect whatsoever.


right ...

But there are a number of things you can do that will preclude the
compiler from making optimizations - which in and of itself is not a bad
thing.

Many compilers today do an excellent job of inlining code and in the
long run, this results in smaller and faster code if the functions are
small enough. However, the compiler can't do much with inlining across
a virtual function call.

So to get back at your example, if you want to see the difference,
create a virtual member function, and test calling it with references or
pointers. If you see a difference, I'll be surprised.

Jul 22 '05 #4

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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.