468,512 Members | 1,607 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 1506
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Tarundeep | last post: by
5 posts views Thread by Jeff Greenberg | last post: by
9 posts views Thread by Jimmy Cerra | last post: by
26 posts views Thread by yb | last post: by
15 posts views Thread by cedgington | last post: by
23 posts views Thread by tonytech08 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.