473,395 Members | 1,742 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,395 software developers and data experts.

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 1701
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********************@rcn.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
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...
5
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...
9
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...
5
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...
11
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...
26
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...
15
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...
23
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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:
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...
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,...

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.