Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > use pointer and not use pointer, which is faster to access data?

Reply
Thread Tools

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

 
 
shuisheng
Guest
Posts: n/a
 
      09-26-2006
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-26-2006
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.
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      09-26-2006

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();

 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      09-26-2006

"shuisheng" <> wrote in message
news: 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.








 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      09-26-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating longword pointer, which method is faster ? Skybuck Flying C Programming 4 05-28-2010 08:56 AM
Re: Which has faster access Binary Search of std::vector or std::set??? John H. C++ 0 03-01-2010 11:05 PM
array index and pointer, which is faster? Xiaohan C Programming 26 03-30-2008 02:28 AM
Which one is faster: method or field access? Chris Berg Java 6 02-22-2005 04:51 AM
Faster access to MDB? MS Access MDB and ASP =?Utf-8?B?UGhpbA==?= ASP .Net 8 01-27-2005 07:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57