Pointers vs References

Discussion in 'C++' started by spekyuman, Jul 10, 2007.

  1. spekyuman

    spekyuman Guest

    What processes occur during a function call? Specifically, how are
    pointers and references treated during this time? What happens to
    their memory and what they reference during this particular time? Here
    are some example functions to help with the elaboration process:

    Object naiveFunction(Object copy)
    {
    // sizeof (copy) => 750 x 2^20 bytes
    return copy;
    }

    void pointerFunction(Object* ptr)
    {
    // sizeof (ptr) => 4 bytes
    // sizeof (*ptr) => 750 x 2^20 bytes
    }

    void referenceFunction(Object& ref)
    {
    // sizeof (ref) => 750 x 2^20 bytes
    }
    spekyuman, Jul 10, 2007
    #1
    1. Advertising

  2. spekyuman wrote:
    > What processes occur during a function call? Specifically, how are
    > pointers and references treated during this time? What happens to
    > their memory and what they reference during this particular time? Here
    > are some example functions to help with the elaboration process:


    Do your own homework, will you?
    Victor Bazarov, Jul 10, 2007
    #2
    1. Advertising

  3. spekyuman

    Rolf Magnus Guest

    spekyuman wrote:

    > What processes occur during a function call?


    Why do you want to know?

    > Specifically, how are pointers and references treated during this time?


    Just like any other time.

    > What happens to their memory and what they reference during this
    > particular time?


    Nothing.
    Rolf Magnus, Jul 10, 2007
    #3
  4. spekyuman

    spekyuman Guest

    On Jul 10, 1:16 pm, "Victor Bazarov" <> wrote:
    > spekyuman wrote:
    > > What processes occur during a function call? Specifically, how are
    > > pointers and references treated during this time? What happens to
    > > their memory and what they reference during this particular time? Here
    > > are some example functions to help with the elaboration process:

    >
    > Do your own homework, will you?


    Number one, do not mouth off on random posts in the illusion of acting
    sophisticated. Why? It displays nothing more than your qualities as
    human... Cough cough [you lack insight]... Elaborating further on the
    main subject to illustrate the point of this post: A reference is an
    alias, or reference [=-O], to another variable which allows you to
    indirectly access its storage. A pointer allows you to do much the
    same and yatta yatta... Since we now share a common knowledge, lets
    talk about the sophisticated processes of sending data to a procedure
    [=-O]. This is where the heart of my question lies:

    [1] Sending data by value to a procedure requires data to be copied
    from the caller to the routine.
    [2] Sending a pointer to a procedure requires data to be copied from
    the caller to the routine. Specifically, the pointer address is sent
    by value and reports a size of 4-8 bytes on modern systems; only the
    object data is being passed by reference.
    [3] My question is, what exactly happens with a reference type during
    a procedure call? Is there any overhead? What are the mechanisms at
    work?
    spekyuman, Jul 10, 2007
    #4
  5. spekyuman wrote:
    > [..] This is where the heart of my question lies:
    >
    > [1] Sending data by value to a procedure requires data to be copied
    > from the caller to the routine.


    From the factual argument into the formal argument. Or, simply put,
    the local variable of the function has to be initialised with the same
    value (copy-initialised, of course).

    > [2] Sending a pointer to a procedure requires data to be copied from
    > the caller to the routine. Specifically, the pointer address


    The pointer address?

    > is sent
    > by value and reports a size of 4-8 bytes on modern systems; only the
    > object data is being passed by reference.


    The pointer is passed by value: the local variable is initialised with
    the value of the pointer that the caller supplies as the argument.
    Nothing else is passed by no other method.

    > [3] My question is, what exactly happens with a reference type during
    > a procedure call?


    The reference (the local variable of the function) is initialised by
    the l-value passed to it by the caller.

    > Is there any overhead? What are the mechanisms at
    > work?


    Some implementation-defined mechanisms. How references are implemented
    and what (if any) overhead there can be observed, is not specified by
    the language. If you'd like to discuss a particular implementation,
    you're welcome to, just not here.

    Initialising a reference argument (an argument that has a reference
    type) is no different than initialising one without the interference
    of a function call/invocation.

    Object someObject;
    Object & rObject = someObject;

    what mechanisms are at work? Is there overhead? Implementation-defined.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 10, 2007
    #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. Roger Leigh
    Replies:
    8
    Views:
    422
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    439
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    592
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    697
    Bruno Desthuilliers
    Dec 12, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page