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 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.
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.
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
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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Dylan |
last post by:
thanks
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |