Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Branskins

macrumors 65816
Original poster
Dec 8, 2008
1,235
180
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 =/
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I am trying to understand something:

int x = 4;
int &val = x;

What is actually going on behind the scenes?

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.

Does val just become another name for the variable x at the same address? With no additional memory allocated?

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

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

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.

Small stuff like this really catch me up =/
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.
 

Branskins

macrumors 65816
Original poster
Dec 8, 2008
1,235
180
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Branskins

macrumors 65816
Original poster
Dec 8, 2008
1,235
180
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

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 =/
 

iSee

macrumors 68040
Oct 25, 2004
3,539
272
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).

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?
Yes, exactly.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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.
 

Branskins

macrumors 65816
Original poster
Dec 8, 2008
1,235
180
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.