Solved

Class functions and memory

Posted on 1998-07-27
7
193 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
ID: 1168805
No.  details coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168806
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
ID: 1168807
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:alexo
ID: 1168808
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
ID: 1168809
>> 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
ID: 1168810
If this has answered your question, you should grade it.
0
 

Author Comment

by:Frost_Byte
ID: 1168811
Sorry for the delay - my service provider was down...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

895 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