473,626 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1816
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
2203
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 myErrorHandler
10
3436
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 somewhere that each folder under the "web site" is compiled in separate assembly. I however, did not find that the "web site" creation in vs.net 2005 created any AssemblyInfo.cs file.
2
1623
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 command to close out the other session?
2
2119
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," and I uncheck it. Everything appears to work fine after that, however, this raises two questions in my mind. 1. Does simply unchecking the missing reference cause me to lose any functionality? I haven't noticed any yet, but that doesn't mean...
15
3777
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 image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
1
1278
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 standpoint to declaring all my string constants up-front as opposed to declaring them in-line? For example: public class ProjectConsts {
35
1905
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
0
252
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: class A { };
30
1896
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 some light on this! (code abbreviated for clarity) I have a parent class, DetailCollection, with a child class KeycodeTracker:
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8644
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6126
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.