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

Discussion in 'C++' started by shuisheng, Sep 26, 2006.

  1. shuisheng

    shuisheng Guest

    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
    shuisheng, Sep 26, 2006
    #1
    1. Advertising

  2. shuisheng

    Ian Collins Guest

    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.
    Ian Collins, Sep 26, 2006
    #2
    1. Advertising

  3. shuisheng

    Salt_Peter Guest

    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();
    Salt_Peter, Sep 26, 2006
    #3
  4. "shuisheng" <> wrote in message
    news:...
    > 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.
    Dave Townsend, Sep 26, 2006
    #4
  5. shuisheng

    peter koch Guest

    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
    peter koch, Sep 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGhpbA==?=

    Faster access to MDB? MS Access MDB and ASP

    =?Utf-8?B?UGhpbA==?=, Jan 26, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    457
    Kevin Spencer
    Jan 27, 2005
  2. Chris Berg
    Replies:
    6
    Views:
    329
    Rhino
    Feb 22, 2005
  3. Xiaohan

    array index and pointer, which is faster?

    Xiaohan, Feb 29, 2008, in forum: C Programming
    Replies:
    26
    Views:
    4,957
  4. John H.
    Replies:
    0
    Views:
    638
    John H.
    Mar 1, 2010
  5. Skybuck Flying

    Calculating longword pointer, which method is faster ?

    Skybuck Flying, May 27, 2010, in forum: C Programming
    Replies:
    4
    Views:
    395
    Phil Carmody
    May 28, 2010
Loading...

Share This Page