Re: code efficiencies with pointers

Discussion in 'C++' started by Victor Bazarov, Jun 24, 2003.

  1. "pw" <> wrote...
    > My last questions on pointers:
    >
    > Is it fair to say that argument/object naming is really a convenience

    (i.e.
    > an aid) to use in place of pointers and references to help people

    understand
    > and write the code? [This leads to the next Q]


    Well, yes, if instead of names we'd use addresses, we'd have, well,
    machine code. High-level languages have this feature, odd as it may
    be, named objects. In programmer lingo they are known as variables.

    > Is it then correct to say that pointers and references are more efficient

    in
    > terms of machine performance, particularly in the case of larger objects.
    > Instead of passing a whole object, you pass the pointer/reference to that
    > object? This would then be a consideration on what is the size of the

    object
    > your are passing?


    Yes, I believe that is correct. If you're passing an int, it's often
    faster to pass by value than otherwise (if you don't need any change
    to it to be visible outside). But for larger objects, passing by ref
    or pointer can be faster. Especially if copying involves invoking some
    kind of function (like a copy c-tor).

    > Pointers and references would then be "essential" for larger code, with
    > arguments OK for small basic code, or small basic objects? Thus, when
    > defining functions, is it better to use reference passing instead of

    object
    > passing?


    That depends. Pointers and references are _essential_ for polymorphism.
    There is no polymorphism without them. Often that involves pure abstract
    interfaces, which by themselves are very light and _could_ be passed by
    value without any performance degradation. However, passing values in
    such case would be disastrous. [ See "slicing" ]

    > Therefore, the sooner I come to understand and use pointers and

    references,
    > the better the programs I would be creating.


    That's true WRT any part of the language.

    Victor
     
    Victor Bazarov, Jun 24, 2003
    #1
    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. Phil
    Replies:
    1
    Views:
    649
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    765
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    459
    Alan Kelon
    Oct 29, 2003
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    680
  5. oldyork90

    regx, global efficiencies?

    oldyork90, Jun 9, 2008, in forum: Javascript
    Replies:
    4
    Views:
    96
    oldyork90
    Jun 10, 2008
Loading...

Share This Page