reference vs pointer

S

sam_cit

Hi Everyone,

A reference is a alias for a variable.

int i =5;
int &b = i;

Is memory allocated for the reference variable like a pointer
variable?
And is it correct that once a reference is set to a reference
variable, it can't be changed again?

Thanks in advance!!!
 
S

Sylvester Hesp

Hi Everyone,

A reference is a alias for a variable.

int i =5;
int &b = i;

Is memory allocated for the reference variable like a pointer
variable?

Implementation defined. For example, if this example was local to a
function, the compiler can easily figure out that by 'b' you actually mean
'i'. And since it is not possible to get the address of a reference (&b
would yield the address of the object referenced) no address is needed.
And is it correct that once a reference is set to a reference
variable, it can't be changed again?

Yes. After it's definition, all operations on the reference work on the
object referenced, not the reference itself. They'll behave just like any
other non-referenced object.

- Sylvester Hesp
 
R

R. Scott Mellow

Hi Everyone,
Hi.

A reference is a alias for a variable.

int i =5;
int &b = i;

Is memory allocated for the reference variable like a pointer
variable?
No.

And is it correct that once a reference is set to a reference
variable, it can't be changed again?

Yes.
 
S

Sylvester Hesp

R. Scott Mellow said:

It's implementation defined, so a compiler _can_ allocate memory like it
would for a pointer. And think about it, if you define a function to take a
reference, how would the caller pass the actual reference? Surely it needs
some way to pass the address of the variable you use as the argument to the
function (and thus most compilers *will* allocate memory like it would for a
pointer).

- Sylvester Hesp
 
P

Puppet_Sock

A reference is a alias for a variable.

int i =5;
int &b = i;

Is memory allocated for the reference variable like a pointer
variable?

As Sylvester said, it is implementation defined. In some cases
there will be some memory used, such as passing a reference
to a function as an argument. A typical way of implementing a
reference is through something very similar to a pointer, but you
should be very reluctant to depend on any such details.
A compiler is free to do it any old way that satisfies the standard,
and the standard does not require it to use memory. Nor any
particular amount of memory if it does.
And is it correct that once a reference is set to a reference
variable, it can't be changed again?

In nearly every case, that is correct. There's a "trivia question"
type situation where a reference can be re-seated. But I'd be
very unhappy to find such a thing in production code. It's more
in the line of something you'd find in an obfuscation contest.
That is to say, even in that very special circumstance where
it's possible, it's almost certainly going to make people frown
furiously at you if you try it.
Socks
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi Everyone,

A reference is a alias for a variable.

int i =5;
int &b = i;

Is memory allocated for the reference variable like a pointer
variable?

No, try this:

int* ip = &i;
int* bp = &b;

if (ip == bp)
std::cout << "Same\n";
And is it correct that once a reference is set to a reference
variable, it can't be changed again?

Yes.
 
S

Sylvester Hesp

Erik Wikström said:
No, try this:

int* ip = &i;
int* bp = &b;

if (ip == bp)
std::cout << "Same\n";

The fact that you get the address of the object referenced to by using the
address-of operator (&) does not prove the actual reference doesn't have an
address itself :)

- Sylvester Hesp
 
S

Scott

Implementation defined. For example, if this example was local to a
function, the compiler can easily figure out that by 'b' you actually mean
'i'. And since it is not possible to get the address of a reference (&b
would yield the address of the object referenced) no address is needed.


Yes. After it's definition, all operations on the reference work on the
object referenced, not the reference itself. They'll behave just like any
other non-referenced object.

- Sylvester Hesp

hi
 
R

Rolf Magnus

Erik said:
No, try this:

int* ip = &i;
int* bp = &b;

if (ip == bp)
std::cout << "Same\n";

What does this have to do with the question if a reference takes up memory?

Basically, you can't do anything to a reference after its definition.
 
G

Guest

What does this have to do with the question if a reference takes up memory?

Well, basically if it takes up memory then it has an address, and using
the address-of operator returns the address of it's operand, so if a
reference did take up memory then the address-of operator would return
its address.
 
O

Obnoxious User

Erik Wikström skrev:
Well, basically if it takes up memory then it has an address, and using
the address-of operator returns the address of it's operand, so if a
reference did take up memory then the address-of operator would return
its address.

Are you sure about that. The implementation/compiler could make the
reference object transparent even though it may take up memory. It
doesn't matter in the end.
 
A

Andre Kostur

Well, basically if it takes up memory then it has an address, and
using the address-of operator returns the address of it's operand, so
if a reference did take up memory then the address-of operator would
return its address.

Not necessarily.... keep in mind that any usage of the reference after the
point of declaration will automatically "resolve" the reference. As a
result, there is no way to indicate to the compiler that you wish to talk
about the reference itself. Any attempt to do so will cause the compiler
to interpret it to mean that you want to talk about the referred-to object.
So when you do (the &b part of):

int a;
int & b = a;

&b;

The "b" portion is automatically rewritten (effectively) to:

&a;
 
V

Victor Bazarov

Erik said:
Well, basically if it takes up memory then it has an address, and
using the address-of operator returns the address of it's operand,

Does it, now?

#include <iostream>
class TakesUpMemory_OrDoesIt
{
public:
TakesUpMemory_OrDoesIt* operator&() const { return 0; }
};

int main()
{
TakesUpMemory_OrDoesIt d, &rd = d;
std::cout << &d << ',' << &rd << std::endl;
}
so
if a reference did take up memory then the address-of operator would
return its address.

Aha...

V
 
S

Sylvester Hesp

Erik Wikström said:
Well, basically if it takes up memory then it has an address, and using
the address-of operator returns the address of it's operand, so if a
reference did take up memory then the address-of operator would return its
address.

Well, then how would you explain the following behaviour ?

struct A
{
int i;
};

struct B
{
int i;
int & j;
B() : j(i) { }
};

int main()
{
std::cout << sizeof(A) << std::endl;
std::cout << sizeof(B) << std::endl;

B b;
std::cout << &b.i << ", " << &b.j << std::endl;
}


- Sylvester Hesp
 
G

Greg Herlihy

Implementation defined. For example, if this example was local to a
function, the compiler can easily figure out that by 'b' you actually mean
'i'. And since it is not possible to get the address of a reference (&b
would yield the address of the object referenced) no address is needed.

Unspecified. If the actual storage requirements (if any) of a referece
were "implementation defined" then the implementation would have to
document whether a reference occupies storage or not. In contrast, the
implementation's "unspecified" behavior does not need to be documented
at all.

Greg
 
J

James Kanze

A reference is a alias for a variable.
int i =5;
int &b = i;
Is memory allocated for the reference variable like a pointer
variable?

Formally, it's not required. Practically, the semantics of
references are designed so that they can be implemented more or
less using a T* const, and in some cases, the compiler may need
for a reference. But there's really no way a conforming program
can tell. (Similarly, if you never take the address of a
T*const---and you can't take the address of a reference---the
compiler is not required to allocate memory for it.)
And is it correct that once a reference is set to a reference
variable, it can't be changed again?

Correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top