Class memory usage

Started by
2 comments, last by 3T 17 years ago
If I have a C++ class containing methods, and there are thousands of instances of this class created when the program is run, will each instance's methods use up memory? I.e. Would it be better to implement large methods (that operate on an instance) outside of the class to save memory? Or are class methods only stored in memory once? Thanks!
Advertisement
Nope, class methods are only stored once. Also, if you're talking about virtual methods, a class which has any virtual methods will have (in most implementations) one extra pointer member (so 4 bytes on 32-bit systems), but again the methods themselves are only stored once.
Methods do not consume memory per object/class-instance. You can think of a method as a function taking a pointer to the object as an additional argument - the 'this' pointer. If you declare a method virtual it will require memory in the virtual function table of that particular class but again _not_ per object/class-instance. So no, writing larger methods instead of many smaller does not save you memory per object/class-instance.
Thanks :)

This topic is closed to new replies.

Advertisement