Jump to content
  • Advertisement
Sign in to follow this  
3T

Class memory usage

This topic is 4080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!