beginner's question on reference variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    beginner's question on reference variable

    I read in textbooks and in this Usenet group that
    a reference is not an object but only an alias.

    consider

    #include <iostream>
    using namespace std;

    int main()
    {
    int obj = 100;
    int &ref = obj;

    cout << ref << endl;

    return 0;
    }

    When this program is run, will there be a memory location created
    for the variable ref ? Or the compiler will replace ref with obj ?

    I do not know how to check whether a variable occupies some
    memory(that is, stored in memory) when a program is run.

    Kindly explain

    Thanks
    V.Subramanian
  • alan

    #2
    Re: beginner's question on reference variable

    On Nov 21, 8:10 pm, "subramanian10. ..@yahoo.com, India"
    <subramanian10. ..@yahoo.comwro te:
    I read in textbooks and in this Usenet group that
    a reference is not an object but only an alias.
    >
    consider
    >
    #include <iostream>
    using namespace std;
    >
    int main()
    {
    int obj = 100;
    int &ref = obj;
    >
    cout << ref << endl;
    >
    return 0;
    >
    }
    >
    When this program is run, will there be a memory location created
    for the variable ref ? Or the compiler will replace ref with obj ?
    The compiler *could* create a pointer to obj, which is "ref". In such
    a case your reference is really a pointer with a different syntax. Or
    it *could* optimize that pointer away (possibly even if you turn off
    optimizations!) , in which case there is no memory allocated for ref;
    each time it sees ref, it replaces "obj".

    And then some people will come and start talking about this-or-that
    points which I myself don't understand.
    >
    I do not know how to check whether a variable occupies some
    memory(that is, stored in memory) when a program is run.
    Note that if you're thinking of memory at this point, you are not
    really thinking C++ yet.

    A reference is just another way of referencing the object. The fact
    that it's *often* implemented as a pointer that *sometimes* takes up
    stack space doesn't matter (at least until you understand the
    something-or-other points the other people talk about). As far as
    you're concerned, it *is* the object. You can mutate its data members
    and call its member functions using ref.something() syntax. You can
    assign something to it via ref = somevalue;. It's like a variable
    variable - you can attach it to some other object at some other time
    (usually done by putting it as a function argument). For example:

    void turnintoten(int & v){
    v = 10;
    }
    int main(){
    int x, y;
    turnintoten(x); //now v in the code above refers to x
    turnintoten(y); //now v in the code above refers to y
    ....
    }

    Note that as far as I know there is no other way of changing a
    reference so that it aliases another object, other than using it as a
    function argument. For example:
    int i, j;
    int& ref = i;
    ref = j; //it is i that is changed; ref still refers to i.


    Comment

    • Mike Wahler

      #3
      Re: beginner's question on reference variable

      <subramanian100 in@yahoo.comwro te in message
      news:a9104fef-b6f1-4e27-aacb-28a91ff5e48f@f3 g2000hsg.google groups.com...
      >I read in textbooks and in this Usenet group that
      a reference is not an object but only an alias.
      Correct, it's simply another name for an already
      existing object, which usually already has a name.
      >
      consider
      >
      #include <iostream>
      using namespace std;
      >
      int main()
      {
      int obj = 100;
      A type 'int' object, whose name is 'obj'.
      int &ref = obj;
      Another name by wich to refer to the object 'obj'.
      (There is *one* object, and there are two names
      you can use to refer to it.).
      >
      cout << ref << endl;
      >
      return 0;
      }
      >
      When this program is run, will there be a memory location created
      for the variable ref ?
      'ref' is *not* a variable. It's simply a name you can use
      to refer to 'obj'.
      Or the compiler will replace ref with obj ?
      When the compiler sees 'ref', it will generate code to
      operate on 'obj'.
      >
      I do not know how to check whether a variable occupies some
      memory(that is, stored in memory) when a program is run.
      'ref' is not a variable. 'obj' is. 'obj' occupies memory of
      sizeof(int) bytes, located at address &obj.
      >
      Kindly explain
      What you are calling a variable is technically called an
      'object' in C++. An object will occupy memory space (in
      your program's memory area). A reference is simply another
      name for the same memory area. 'Behind the scenes', your
      C++ implementation might (and probably does, but isn't
      required to) use memory to make the reference work, but
      from your program's perspective, it does not occupy any memory.


      -Mike


      Comment

      • subramanian100in@yahoo.com, India

        #4
        Re: beginner's question on reference variable

        On Nov 21, 5:35 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
        What you are calling a variable is technically called an
        'object' in C++. An object will occupy memory space (in
        your program's memory area). A reference is simply another
        name for the same memory area. 'Behind the scenes', your
        C++ implementation might (and probably does, but isn't
        required to) use memory to make the reference work, but
        from your program's perspective, it does not occupy any memory.
        >
        -Mike
        I read that we cannot have array of references
        because a reference is not an object. Is it
        mentioned from the program's perspective which
        you have defined above ?

        Please bear with me for asking it again. The reason
        for asking is that if the reference name uses some
        stack space, then why can't we have an array of
        references ?

        Kindly clarify

        Thanks
        V.Subramanian

        Comment

        Working...