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

use pointer and not use pointer, which is faster to access data?

Dear all,

Would you please tell me when using pointer and not using pointer,
which is faster to access data? Such as

float *pVal
float val

MyClass *pA pA->member1
MyClass a a.member1

Thanks,

Shuisheng

Sep 26 '06 #1
4 1752
shuisheng wrote:
Dear all,

Would you please tell me when using pointer and not using pointer,
which is faster to access data? Such as

float *pVal
float val

MyClass *pA pA->member1
MyClass a a.member1
What did your measurements show?

--
Ian Collins.
Sep 26 '06 #2

shuisheng wrote:
Dear all,

Would you please tell me when using pointer and not using pointer,
which is faster to access data? Such as

float *pVal
float val

MyClass *pA pA->member1
MyClass a a.member1

Thanks,

Shuisheng
MyClass * pA is not an object, its just a pointer. It would be clearer
if you supplied code like this:

int main()
{
MyClass a;
MyClass* p = &a;
// which is faster?
a.member();
p->member();
}

And the answer is a.member() since it does not require an object to be
instantiated like a pointer does. A pointer is nothing until it
actually points to something. Think about it, the following is
undefined behaviour.

int main()
{
MyClass* p;
p->member();
}

Perhaps the real question should be: which one is safer? In which case
i'ld suggest considering an alternative:

MyClass a;
MyClass& ref( a );
ref.member();

Sep 26 '06 #3

"shuisheng" <sh*********@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Dear all,

Would you please tell me when using pointer and not using pointer,
which is faster to access data? Such as

float *pVal
float val

MyClass *pA pA->member1
MyClass a a.member1

Thanks,

Shuisheng
Probably doesn't make any difference in most cases since under-the-covers
the two mechanisms would generate similar code.

The real question would be the stylistic one. Both pointers and
references model aliases to other objects,
with the case of pointer, the object might be nil, ie, it can be missing,
whereas with a reference it must represent a valid object.

If you use a reference, it is unecessary to check for the validity of the
object being referenced, it must exist ( unless you've done some tricky
things to sabotage this invariant): ie,

void doFoo( Foo& foo )
{
// do something with foo..
}

void doFoo(Foo* foo )
{
if ( foo != 0)
{
// do something with foo.
}
}

I'd prefer to use the reference when I can since it make my code clearer and
also eliminates some unnecessary checking.


Sep 26 '06 #4

shuisheng wrote:
Dear all,

Would you please tell me when using pointer and not using pointer,
which is faster to access data? Such as

float *pVal
float val

MyClass *pA pA->member1
MyClass a a.member1

Thanks,

Shuisheng
On the processors I know best (x86 family) the direct access is faster,
but the important question is why do you ask? You should always declare
by value, declaring by pointer only if you have to. That rule would be
valid even if access via a pointer was faster.

/Peter

Sep 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
10
by: Willem | last post by:
Looking for some opinions on alternatives to programming with Access. I find that quite often I need to loop through my recordsets (first to last) performing calculations and was wondering if...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
9
by: Winston Li | last post by:
In the Brain and Denis' book "The C Programming Language" Section 5.3 "Pointes abd Arrays", a statment goes: "Any operation that can be achieved by array subscripting can also be done with...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
3
by: avinashphilip | last post by:
array or pointer is faster access? i feel array is faster, if any conflict please help me
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.