Question regarding references.

R

Renji Panicker

I have a question regarding references. Consider the following code:
<code>
#include <iostream>
class A
{
int _a1;
int _a2;
int _a3;
};

class B
{
public:
B(A& a) : _a(a){}
int _b;
A& _a;
};

int main(int argc, char* argv[])
{
A a;
B b(a);
std::cout << sizeof(a) << ", " << sizeof(b) << std::endl;
}
</code>

Having understood that references are equivalent to just an additional
entry in the symbol table, I am under the impression that the member
B::_a should not take up any runtime memory, that is, it should just
be a symbol pointing to the original instance of A to which B::_a was
initialized.

Hence I was expecting the output to be 12, 4. Instead what I got was
12, 8. Which means, the reference variable is also taking up memory (4
bytes) at runtime.

Could someone explain (or point me to a reference) this? I would
appreciate it.
Thanks,
-/renji
Renji Panicker
PS: I am using gcc 4.1.3, Ubuntu 7.10
 
I

Ian Collins

Renji said:
I have a question regarding references. Consider the following code:
<code>
#include <iostream>
class A
{
int _a1;
int _a2;
int _a3;
};

class B
{
public:
B(A& a) : _a(a){}
int _b;
A& _a;
};

int main(int argc, char* argv[])
{
A a;
B b(a);
std::cout << sizeof(a) << ", " << sizeof(b) << std::endl;
}
</code>

Having understood that references are equivalent to just an additional
entry in the symbol table, I am under the impression that the member
B::_a should not take up any runtime memory, that is, it should just
be a symbol pointing to the original instance of A to which B::_a was
initialized.
How can something take zero space? Each instance of the class has a
unique reference member, so it has to be part of the class.
 
S

Stuart Redmann

Renji said:
I have a question regarding references. Consider the following code:
<code>
#include <iostream>
class A
{
int _a1;
int _a2;
int _a3;
};

class B
{
public:
B(A& a) : _a(a){}
int _b;
A& _a;
};

int main(int argc, char* argv[])
{
A a;
B b(a);
std::cout << sizeof(a) << ", " << sizeof(b) << std::endl;
}
</code>

Having understood that references are equivalent to just an additional
entry in the symbol table,

Note that this is highly compiler-dependent and can only be achieved if the
reference can be bound at compile-time. If this is not possible (as in your
particular case) the compiler will implement the reference by an additional
pointer variable. Note that the decision whether to implement the reference via
a pointer or not does not only depend on the compiler you use but also on the
compiler options.

In your case, the compiler has no other choice than using a pointer as it has to
keep some the address of the passed A object somewhere. A different case would
be if class B looked like the following:

class B
{
public:
int _b;

// A copy of a.
A _a;

// A reference to the copy.
A& _ref_a;

B(A& a)
: _ref_a (_a),
_a(a)
{}
};

Here the compiler _may_ optimize away the reference (it knows that the symbols
B::_a and B::_ref_a are essentially the same). An interesting question would be
whether a struct may have different sizes depending on the compiler options, or
if the standard mandates that member references must always be implemented by
pointers.
I am under the impression that the member
B::_a should not take up any runtime memory, that is, it should just
be a symbol pointing to the original instance of A to which B::_a was
initialized.

The (one and only) symbol table has exactly one entry for B::_a, but the various
objects of class B will certainly refer to various other instances of class A.
Anyway, the symbol table is only an internal implementation detail of compilers,
and it is not part of the actual generated code.
Hence I was expecting the output to be 12, 4. Instead what I got was
12, 8. Which means, the reference variable is also taking up memory (4
bytes) at runtime.

Could someone explain (or point me to a reference) this? I would
appreciate it.

Regards,
Stuart
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top