Solved

Class functions and memory

Posted on 1998-07-27
7
195 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 279
White board coding practice 3 93
Error creating a new C++ project in ,net 20 51
C++ help/ Toy problem 19 50
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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