473,385 Members | 1,320 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,385 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 1787
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.