Question about references

Discussion in 'Mac Programming' started by Branskins, Jul 1, 2010.

  1. Branskins macrumors 65816

    Joined:
    Dec 8, 2008
    #1
    I am trying to understand something:

    int x = 4;
    int &val = x;

    What is actually going on behind the scenes? Does val just become another name for the variable x at the same address? With no additional memory allocated?

    Does x give an address to val implicitly? Is x automatically promoted to the same reference to int type as val?

    Small stuff like this really catch me up =/
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    The number of bytes needed to store an integer are allocated on the stack for x. This memory is set to the value 4.

    The second line is nonsense, so I'll assume you meant:
    Code:
    int *val = &x;
    This allocates the number of bytes needed to store a pointer on the stack for val, then gets the memory address where x was allocated and sets the memory allocated for val to the value of the address allocated on the stack for x.

    Sort of, see above. Changing x or *val will both result in the memory allocated for x being changed.

    I don't really understand these questions. Nothing is really happening "implicitly" here other than what the compiler would do for any variables (setting up memory for them, etc.). There is no casting or promotion here, &x evaluates to type int *, which is the type of val.

    Good. If you're going to bother with something like C, it's worth learning the mechanics. I consider it a gateway to understanding what's "really happening" at the microarchitectural level.

    -Lee

    Edit:
    if
    Code:
    int &val = x;
    
    is valid C++ or something, apologies. It looks like i would expect a reference argument to a function to look in C++, but not any type of declaration I'm familiar with.
     
  3. Branskins thread starter macrumors 65816

    Joined:
    Dec 8, 2008
    #3
    It is valid in C++. I didn't know it was a new syntax.

    It looks like
    int &val = x;

    makes val an alias for x. They are the exact some object and share the same memory address.

    I guess my confusion stems from how x is promoted to something like a reference to an int. I don't really understand what is going on to make
    "int &val = x" a valid expression. Does x give its address to val?

    Or is this some weird thing done in C++ that no one really questions?

    I was going to then extend this conversation into what returning a reference exactly means:

    int &times2(int &val) {
    val = val *2;

    return val;
    }

    times2(x); //a function call that might not even be valid

    I understand what happens to x but I don't understand the nature of returning a reference. What are your returning?

    If this is isolated to C++ and not Obj-c then it might not be very important, but it has been bugging me. I don't really understanding what is being passed back.

    Returning a pointer to something makes more sense to me, as you are just returning an address of the object.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Oops, sorry. Not a C++ expert at all.
    Code:
    #include <iostream>
    
    int main(int argc, char *argv) {
      int x = 4;
      int &val = x;
      std::cout << "x: " << x << " val: " << val << " &x: " << &x << " &val: " << &val << std::endl;
    }
    
    Displays:
    x: 4 val: 4 &x: 0xbffffb5c &val: 0xbffffb5c

    So it looks like this is just an aliasing/referencing syntax in C++. I'd say that this is just the way things are in C++, and assigning x is really just going to do all of the tricky pointer stuff for you in a "hidden" manner so you don't have to worry about it. I'm guessing the same thing is actually happening behind the scenes, grabbing addresses, etc. You just get a convenient way to deal with it. As for returning a reference, I have absolutely no idea.

    -Lee
     
  5. Branskins thread starter macrumors 65816

    Joined:
    Dec 8, 2008
    #5
    I think you are correct about the hidden stuff going on behind the scenes. Unfortunately all of this hidden stuff has caused a great deal of confusion for me. Now that I know that something may be going on, it makes a little more sense.

    Hopefully I don't have to worry about this too much in Objective-C =/
     
  6. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #6
    If you understand pointers then I can tell you this: C++ references work just like pointers but use the syntax of a regular variable.

    e.g.,
    Code:
    int x = 4;
    int &val = x;
    val = 7;
    is equivalent to:
    Code:
    int x = 4;
    int *val = &x;
    *val = 7;
    In fact, the machine code generated by the compiler will probably be identical.

    One interesting difference is that, unlike pointers, references must be initialized (the compiler will generate an error).

    Yes, exactly.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    http://www.cprogramming.com/tutorial/references.html
    this seems to be a pretty good discussion of reference variables. It alludes to the implemention being done with pointers behind the scenes, but it sounds like the standard doesn't specify, and you shouldn't need to know how they are implemented.

    As for your times2 example, this looks fine to me. It's a bit redundant since you changed the variable passed in, but it should be fine and scoping should be OK since what you return is a reference that was passed in.

    None of this is C, and Objective-C doesn't add reference variables, so this won't be relevant to Objective-C unless you're mixing in C++.

    -Lee
     
  8. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #8
    To be fair, the OP didn't say his question was about C or Objective-C.

    Executive summary: References are like pointers, only they cannot be uninitialized (the compiler will check for you). So you don't need to check for null pointers in your code.

    BTW, your times2() function is a little strange - either you return the value, or you modify the argument. Normally you wouldn't do both.
     
  9. Branskins thread starter macrumors 65816

    Joined:
    Dec 8, 2008
    #9
    Sorry about the example! It was just a quick thing to try and understand what returning a reference exactly means! I don't think I would ever actually do that when actually programming :p

    Thanks everyone! I didn't learn until later that this isn't present in C or objective-c, but the implementation of it was still getting me!
     

Share This Page