Solved

Class functions and memory

Posted on 1998-07-27
7
191 Views
Last Modified: 2010-04-02
This is a simple one but ...:
I'd like to know once and for all, if I have a class with several member functions (just regular functions). If I create several objects of that class, I know the data members are duplicated for each object (memory is allocated for each var, in each object), but, are the functions _themselves_ duplicated as well ? Is every object taking up space for both it's data AND it's functions ? If so, how can I avoid this ?
Thanks
0
Comment
Question by:Frost_Byte
  • 5
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
Comment Utility
No.  details coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Member functions appear to be part of an object because they are accessed with the same syntax used to access data member.  However functions are not stored with the objects.  (This actually would be implimentation defined, that is, someone could write a C++ compiler that did store the functions with the objects.  However, I can pretty much guarantee that it has never and will never be done!)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Note that if a class is publically derived from a base class, the public functions of the base class can also be used on objects of the derived class.  Again this would be implimentation defined, but I would bet that no implimentations ever written would provide seperate copies of the function for the base and derived class.  

Also note that static data members are not stored in objects of the class.  If a class has a static data member, only one copy of that data member exists and is shared by all the objects that are part of the class.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Expert Comment

by:alexo
Comment Utility
One missed detail:

Generally, an object of a class with no non-static data members will take no memory.
However, if the class defines *virtual functions*, the object will contain a pointer to the vtable.  Similarly, objects of a class with *virtual inheritance* will contain pointers to the virtual base part.

OK, Todd, find your way out of this one :-)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> Generally, an object of a class with no non-static data members will take no memory.
The objects won't need any memory, but they will have it.  Objects are forbidden to be 0 bytes long.

As for the virtual table pointer, that is the point.  rather than storing seperate functions for each object (which could be done to impliment virtual functions) it stores a single virtual table pointer.  rather than copies of the functions.

As for "virtual inheritance" I'm not sure how that fits into this, but that's okay because when I think about multiple inheritance my head hurts anyways.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
If this has answered your question, you should grade it.
0
 

Author Comment

by:Frost_Byte
Comment Utility
Sorry for the delay - my service provider was down...
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now