pass by reference

Discussion in 'C++' started by mike7411@gmail.com, Nov 30, 2006.

  1. Guest

    I was just wondering what typically happens under the hood when you
    pass a C++ variable by reference. It looks like a pointer gets passed
    in, but the compiler let's you access it as if it wasn't a pointer.

    Anyone know if this is how it's always done?
    , Nov 30, 2006
    #1
    1. Advertising

  2. Salt_Peter Guest

    wrote:
    > I was just wondering what typically happens under the hood when you
    > pass a C++ variable by reference. It looks like a pointer gets passed
    > in, but the compiler let's you access it as if it wasn't a pointer.
    >
    > Anyone know if this is how it's always done?


    To think of a reference as a pointer is not healthy.
    The best way to explain a reference is by examining references from the
    same perspective as he who created C++ (Bjarne Stroustrup). After all,
    it was his idea. You might consider looking up his explanation of the
    reference.

    If you declare a variable n:
    int n(5); // or int n = 5;

    n is a reference to what would otherwise have been an *anonymous*
    allocation of a specific type. It basicly sets aside a name or
    identifier that is permanently linked to that allocation. So, on the
    outset, n is technically already a reference. Just like:

    int& r(n); // r is itself a reference to n

    The difference between a reference and a pointer is best examined by
    considering the following:

    // a constant pointer to a mutable object
    int* const p_n = &n;

    I can modify what is *at* p_n but i cannot reseat p_n to point to
    another variable. The same applies to a reference. Note that a
    reference that "refers" to a null value or an invalid value is
    undefined behaviour. The same is not true of a pointer. Pointers are
    dumb. A pointer can be nullified, it can point to nothing in particular
    and it can be casted, etc.

    A pointer has a value - it stores an address and all it knows is that
    its got a type. A reference's "value" is obtained from its referred_to
    object. A reference's address is again the referred_to object's
    address.

    Bottom line is that a reference and a pointer (even a const ptr) have
    very little in common.
    Salt_Peter, Dec 1, 2006
    #2
    1. Advertising

  3. wrote:

    > I was just wondering what typically happens under the hood
    > when you pass a C++ variable by reference.
    > It looks like a pointer gets passed in
    > but the compiler let's you access it as if it wasn't a pointer.
    >
    > Anyone know if this is how it's always done?


    A pointer and a reference are passed the same way --
    a [virtual] machine address.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    E. Robert Tisdale, Dec 1, 2006
    #3
  4. :

    > I was just wondering what typically happens under the hood when you
    > pass a C++ variable by reference. It looks like a pointer gets passed
    > in, but the compiler let's you access it as if it wasn't a pointer.
    >
    > Anyone know if this is how it's always done?



    Short answer:

    If the function call is inlined, there'll be no pointer. If the function
    call is outline, then there's probably a pointer behind the scenes.

    Of course, the C++ Standard doesn't require any particular method, just so
    long as the program behaves as it should.

    --

    Frederick Gotham
    Frederick Gotham, Dec 1, 2006
    #4
    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. Jerry
    Replies:
    20
    Views:
    7,902
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    542
  3. Mr A
    Replies:
    111
    Views:
    2,073
  4. Robert
    Replies:
    10
    Views:
    1,359
    E. Robert Tisdale
    Aug 24, 2005
  5. LuB
    Replies:
    6
    Views:
    445
    Mike Wahler
    Sep 23, 2005
Loading...

Share This Page