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
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.
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
> 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