Re: memory usage of Virtual function poiters
From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/07/04
- Next message: Francis Glassborow: "Re: memory usage of Virtual function poiters"
- Previous message: Francis Glassborow: "Re: Attempt at fixing the issue."
- In reply to: Paul: "memory usage of Virtual function poiters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Francis Glassborow: "Re: memory usage of Virtual function poiters"
- Previous message: Francis Glassborow: "Re: Attempt at fixing the issue."
- In reply to: Paul: "memory usage of Virtual function poiters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|