Objects functions memory consumption
-
I am writing a program with alot of data structures and using objects for this purpose. For some of these data structures I am using a huge number of objects so I don't want to waste memory unnecisarily with each object. Here's my question, does it make any difference in terms of memory consumption if I put some of the functions for manipulating these objects inside the class for that object or in a seperate class or module that I only make only one copy of? In other words does each copy of an object have a reference to its functions or does the compiler seperate them internally. thanks for your help
-
I am writing a program with alot of data structures and using objects for this purpose. For some of these data structures I am using a huge number of objects so I don't want to waste memory unnecisarily with each object. Here's my question, does it make any difference in terms of memory consumption if I put some of the functions for manipulating these objects inside the class for that object or in a seperate class or module that I only make only one copy of? In other words does each copy of an object have a reference to its functions or does the compiler seperate them internally. thanks for your help
MikeMarq wrote:
does it make any difference in terms of memory consumption if I put some of the functions for manipulating these objects inside the class for that object or in a seperate class or module that I only make only one copy of?
No. Encapsulating code in the class is beneficial for design, however, and is one of the key benefits of object-oriented programming.
MikeMarq wrote:
In other words does each copy of an object have a reference to its functions or does the compiler seperate them internally.
I know C++ and C#, and for those languages: All instances of a class/struct are "object"s Only one copy of the code exists, whether it's class/struct method code or code outside of a class/struct. For code "inside the class", an implicit "this" pointer, which is a pointer/reference to the object the code is being called on, so the code runs in the context of that object (meaning only one copy of the code is needed). An object doesn't need a reference to its code...it IS the reference. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: