Question regarding references.



 DEVELOP > c-Plus-Plus > Question regarding references.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Renji Panicker"
Date: 08 Nov 2007 01:29:55 AM
Object: Question regarding references.
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
.

User: "Stuart Redmann"

Title: Re: Question regarding references. 08 Nov 2007 03:18:14 AM
Renji Panicker wrote:

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
.

User: "Ian Collins"

Title: Re: Question regarding references. 08 Nov 2007 01:42:53 AM
Renji Panicker wrote:

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


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER