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