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

S

shuisheng

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
 
I

Ian Collins

shuisheng said:
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?
 
S

Salt_Peter

shuisheng said:
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();
 
D

Dave Townsend

shuisheng said:
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.
 
P

peter koch

shuisheng said:
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
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top