473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Nov 21 '07 #1
3 1248
On Nov 21, 8:10 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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.
Nov 21 '07 #2
<su**************@yahoo.comwrote in message
news:a9**********************************@f3g2000h sg.googlegroups.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
Nov 21 '07 #3
On Nov 21, 5:35 pm, "Mike Wahler" <mkwah...@mkwahler.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
Nov 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chimi | last post by:
Is it possible to reference an index of an associative array using a variable? e.g.: If I had an array: $myArray = "myValue"; and if I were to declare a variable: $myVariable = "first"; ...
9
by: Birgit Rahm | last post by:
Hello newsgroup, I am a beginner, so I am asking maybe immoderate questions. I want to delete a variable, after filling it with complex objects. How should I do it? e.g. AAA = AAA = Can I...
13
by: Martin | last post by:
I have 5 days of experince with XSLT and I am sure my problem is pretty much as basic as they come but I cannot work it out, so I hope someone will take pity on me - please! I have inherited an...
15
by: Pelle Beckman | last post by:
Hi all, I have a few newbie questions: In function declaration what does a 'const' mean inside the parameter list ? That it won't modify the value? void MemberFunction(const int x);
2
by: Keith | last post by:
Good Afternoon, New to .Net. I am trying to pass date/time values to a MS Access query depending on what value is selected from a dropdown list box (January, February, etc). I have declared...
6
by: Chris | last post by:
Hi all, I've recently discovered the joys of tag :) I have a ListView which is populated with strings from a List<>, and each ListView item is linked to the relevant object in List<> using Tag....
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
9
by: cheng | last post by:
I was trying to figure out how & work, so I wrote the following code #include <iostream> using namespace std; void print(int &); int main() { int i = 10;
3
by: Alex | last post by:
Hi Everyone, I'm working through the Beginning Visual Basic 2005 book, but I have a question that though might be obvious, I'm not seeing an explanation. THe first example in the book is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.