By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

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

P: n/a

"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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.