?
Solved

Class functions and memory

Posted on 1998-07-27
7
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 120 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

764 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