Connecting Tech Pros Worldwide Forums | Help | Site Map

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

shuisheng
Guest
 
Posts: n/a
#1: Sep 26 '06
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


Ian Collins
Guest
 
Posts: n/a
#2: Sep 26 '06

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


shuisheng wrote:
Quote:
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.
Salt_Peter
Guest
 
Posts: n/a
#3: Sep 26 '06

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



shuisheng wrote:
Quote:
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();

Dave Townsend
Guest
 
Posts: n/a
#4: Sep 26 '06

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



"shuisheng" <shuisheng75@yahoo.comwrote in message
news:1159235794.136157.167980@e3g2000cwe.googlegro ups.com...
Quote:
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.








peter koch
Guest
 
Posts: n/a
#5: Sep 26 '06

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



shuisheng wrote:
Quote:
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

Closed Thread