Re: memory usage of Virtual function poiters

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/07/04


Date: Fri, 7 May 2004 12:04:51 +0100

In message <5dab091d.0405061333.411142dd@posting.google.com>, Paul
<paulmmm01@hotmail.com> writes
>Hi there I have a question about the above subject.
>
>(phsuedo code))
>Say we have a class with 20 odd member variables i.e:
>class TheClass{
>int i1;
>char i2;
>double i3;
>bool i4;
>float i5;
>/*etc*/
>
>void foo(int){}
>void foo(char){}
>void foo(double){}
>void foo(bool){}
>void foo(float){}
>/*etc*/
>};

OK so the class has 20 data members, presumably because they are useful.
>
>And as an alternative we had another class which had 20 virtual
>functions like so:
>class Base{
>virtual void foo(int){}
>virtual void foo(char){}
>virtual void foo(double){}
>virtual void foo(bool){}
>virtual void foo(float){}
>/* etc*/
>};

This appears to have lots of functions but no data members. The cost of
the virtual functions is effectively 20 + n function ptrs per class
(i.e. for each derived class + the base class) where n is a fixed amount
to handle such things as RTTI. In addition each instance (object) will
have an extra hidden data pointer (the pointer to the virtual function
pointer table)

>
>template<class C>
>class TheClass: public Base{
>template<typename T>
>void foo(T){}
>C _c;
>};

In this case each template instantiation will have only a single data
member which begs the question as to why you had 20 originally. That
also means that each different data type will have a complete vfpt
which, unless there are many instances will be more resource hungry.
>
>
>In the second example there is only one variable needed but is there a
>cost for having the virtual function table?

Yes, but also how could this design meet the same needs as the previous
one? In addition how would all those virtual functions be of any use?

>
>For example if I were to create a large array of TheClass's would I be
>better of using the first method or the second or is there no real
>difference? Does the virtual function table cancel out what you appear
>to be saving on variables?

I think you are asking questions that are impossible to answer because
the two designs are as different as chalk and cheese. Before even
thinking about optimisation issues you need to get the design right for
the job being done.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Interview Questions
    ... What is a pure virtual function, and why would you use it? ... class Super1: public Sub ... void DoSomething ... while (cur!= SubVector.end()) ...
    (microsoft.public.vc.mfc)
  • Re: memory usage of Virtual function poiters
    ... > void foo ... The pointer points to table of pointers, ... address of the function to call for each defined virtual function. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Hmm... inheritence... hmmm
    ... is a class Dog or any derived from it. ... would work without throwing a bad_cast exception. ... types are compatible, then calls the virtual function of the class, which ... void mate{ ...
    (comp.lang.cpp)
  • Re: Sorting a CList
    ... You are correct about the virtual function requirement. ... use dynamic_cast on a pointer to pointer of type X**, ... cannot cast from void*, so when you start from void*, as the sort ... will have this initial unchecked cast at the very least, ...
    (microsoft.public.vc.mfc)
  • Please comment on the code
    ... For B to call back a member of A, the code is design as: ... void docallback(); ...
    (comp.lang.cpp)