473,799 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2065

"calvin" <ca******@opera mail.com> wrote in message news:38******** *************** ***@posting.goo gle.com...
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.newshostin g.com...

"calvin" <ca******@opera mail.com> wrote in message news:38******** *************** ***@posting.goo gle.com...
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.merke rk(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
9969
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 must be an object instead of
9
3005
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 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
18
3542
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
5405
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
2684
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
4477
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
2403
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 (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
11
4244
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
29
3656
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
41
3689
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 pointer and reference are and how they relate to each other.
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10247
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.