473,399 Members | 4,192 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,399 software developers and data experts.

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

Nov 8 '07 #1
2 1790
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.
Nov 8 '07 #2
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
Nov 8 '07 #3

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

Similar topics

3
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
2
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
2
by: jodyblau | last post by:
I have noticed that when I move my database onto a different computer, I often get a message about some reference missing. So I go into the reference list, and find the one that says "Missing,"...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
1
by: DLN | last post by:
Hello all, I have a quick question regarding how best to use static strings in my C# code that I'm hoping someone can help me with. Is there any advantage/disadvantage from a performance...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
0
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: class A { };
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.