473,795 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object member functions and runtime memory consumption

Hi,

I have a question regarding memory consumption of class/object member
functions.

Say that I have:
class A {
char szAbc[16];
char* getString();
}

Then I have:
vector<A> vA;
// vA.resize(100); or populate an array in other ways.

Right there, I understand that there are multiple copies of szAbc in the
memory. But what about the getString() member function? Are there multiple
copies of getString() function code in the memory? What if I have another
vector<A> vA2. Will they share the same function code in the memory?

If not, what do I to force them to share the very same member function code
in the memory?

Thanks in advance,
--
He Shiming
Jul 23 '05 #1
5 1725
He Shiming wrote:
Hi,

I have a question regarding memory consumption of class/object member
functions.

Say that I have:
class A {
char szAbc[16];
char* getString();
}

Then I have:
vector<A> vA;
// vA.resize(100); or populate an array in other ways.

Right there, I understand that there are multiple copies of szAbc in the
memory. But what about the getString() member function? Are there multiple
copies of getString() function code in the memory?
No.

What if I have another
vector<A> vA2. Will they share the same function code in the memory?


Yes.
Each class has (in the canonical implementation) a table of pointers to
the definitions of the class's member functions, so each (non-inline)
function definition only needs to exist in one place. If a class has
virtual methods, though, each instance of the class may be given its own
pointer to the vtable.
Jul 23 '05 #2
Thank you for the explanation. That was quite helpful.

--
He Shiming

"Jeff Schwab" <je************ @rcn.com> wrote in message
news:Bv******** ************@rc n.net...
He Shiming wrote:
Hi,

I have a question regarding memory consumption of class/object member
functions.

Say that I have:
class A {
char szAbc[16];
char* getString();
}

Then I have:
vector<A> vA;
// vA.resize(100); or populate an array in other ways.

Right there, I understand that there are multiple copies of szAbc in the
memory. But what about the getString() member function? Are there
multiple copies of getString() function code in the memory?


No.

What if I have another vector<A> vA2. Will they share the same function
code in the memory?


Yes.
Each class has (in the canonical implementation) a table of pointers to
the definitions of the class's member functions, so each (non-inline)
function definition only needs to exist in one place. If a class has
virtual methods, though, each instance of the class may be given its own
pointer to the vtable.

Jul 23 '05 #3
Jeff Schwab schrieb:
Each class has (in the canonical implementation) a table of pointers to
the definitions of the class's member functions, so each (non-inline)
function definition only needs to exist in one place. If a class has
virtual methods, though, each instance of the class may be given its own
pointer to the vtable.

Not entirely correct. That table of pointers (the vtable) only exists
at all if the function has virtual methods. Otherwise, such a table
only exists at compile time, as part of the compiler's lookup table to
resolve member function calls - however that is implemented exactly
depends on the compiler of course. In the generated code, the a
non-virtual member function call is no different than a free function
call save for the additional "hidden" this argument and maybe a
different way to pass arguments. No vtable indirection involved.

Cheers,
Malte
Jul 23 '05 #4
Malte Starostik wrote:
Jeff Schwab schrieb:
Each class has (in the canonical implementation) a table of pointers to
the definitions of the class's member functions, so each (non-inline)
function definition only needs to exist in one place. If a class has
virtual methods, though, each instance of the class may be given its own
pointer to the vtable.


Not entirely correct. That table of pointers (the vtable) only exists
at all if the function has virtual methods. Otherwise, such a table
only exists at compile time, as part of the compiler's lookup table to
resolve member function calls - however that is implemented exactly
depends on the compiler of course. In the generated code, the a
non-virtual member function call is no different than a free function
call save for the additional "hidden" this argument and maybe a
different way to pass arguments. No vtable indirection involved.


Thanks, that's a good point.

I wonder whether a table of pointers to member functions can be forced
to exist in the object code? Ordinary functions can be given external
linkage ("extern"), but what about member functions?
Jul 23 '05 #5
Jeff Schwab schrieb:
I wonder whether a table of pointers to member functions can be forced
to exist in the object code? Ordinary functions can be given external
linkage ("extern"), but what about member functions?


Non-inline member functions have external linkage already. So they are
included in whatever symbol table is used on a given platform to resolve
any kind of symbols with external linkage. It just won't be (for the
compilers I know) a distinct per-class table. The mangled function name
includes the class name though, so if you need to build such a table,
you can parse the object file yourself to enumerate all symbols and
filter out those not belonging to the class you're interested in :-)

Cheers,
Malte
Jul 23 '05 #6

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

Similar topics

18
2549
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual functions. Since the very first location of the object memory points to the vtbale hence that should be 32 bit.
5
2126
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
9
1604
by: Jimmy Cerra | last post by:
I am a little confused how the memory for objects is allocated in JavaScript. David Flanagan, in "JavaScript: The Definitive Guide," states that each property of a class takes up memory space when instantiated. So space is created for aMethod three times in this example: // Example 1 function aMethod() {/*stuff*/}; function AClass() { /*stuff*/
5
1938
by: Markus Dehmann | last post by:
Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed. What is the best way to optimize in such a case, both for memory and speed (speed maybe more important)? One idea is to make the class very small, to contain only the most important member data, and put everything else, including member functions, into another class:
11
3846
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
26
5694
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized several parts of the DOM, but this does not include the window object. Thank you
15
8328
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different ways, and I'm confused about the lifetime of objects and whether or not I should be calling delete. Here are two examples: ref class SYMBOL : public IComparable { public: // Constructor / destructor
23
3152
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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
10164
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,...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
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
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.