how to "save" memory on creating array of class with virtual function

D

dbtouch

Dear C++ Users,

I was facing a challenge in my application: we have a class, say,
CFoo, with virtual function. We need create a large a array of CFoo in
the application, CFoo array[10000]. Because of vptr of CFoo, we ended
up with 80000 bytes memory to store vptrs. Is there a better way in
design or technically to save memory while keeping the ploymorphism? I
can only think of using function pointer instead.

Thanks,

dbtouch
 
A

Alf P. Steinbach

* dbtouch:
Dear C++ Users,

I was facing a challenge in my application: we have a class, say,
CFoo, with virtual function. We need create a large a array of CFoo in
the application, CFoo array[10000]. Because of vptr of CFoo, we ended
up with 80000 bytes memory to store vptrs. Is there a better way in
design or technically to save memory while keeping the ploymorphism? I
can only think of using function pointer instead.

You might consider the FlyWeight pattern.

Essentially, separate state and functionality, bring the functionality to the state.

In array store only state.


Cheers & hth.,

- Alf
 
D

dbtouch

Hi, Alf

So I can implement CFoo like:

class FuncCFoo {
public:
virtual void work();
};

class CFoo {
int m_data;
//... all the state data
static FuncCFoo* ptr; // pointer points to FuncCFoo which is a base
class of CFoo functionality
public:
void work() {
ptr->work();
}
};

In this case, I only add 8 bytes. Any comments?

dbtouch
* dbtouch:
Dear C++ Users,
I was facing a challenge in my application: we have a class, say,
CFoo, with virtual function. We need create a large a array of CFoo in
the application, CFoo array[10000]. Because of vptr of CFoo, we ended
up with 80000 bytes memory to store vptrs. Is there a better way in
design or technically to save memory while keeping the ploymorphism? I
can only think of using function pointer instead.

You might consider the FlyWeight pattern.

Essentially, separate state and functionality, bring the functionality to the state.

In array store only state.

Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* dbtouch:
* dbtouch:
Dear C++ Users,
I was facing a challenge in my application: we have a class, say,
CFoo, with virtual function. We need create a large a array of CFoo in
the application, CFoo array[10000]. Because of vptr of CFoo, we ended
up with 80000 bytes memory to store vptrs. Is there a better way in
design or technically to save memory while keeping the ploymorphism? I
can only think of using function pointer instead.
You might consider the FlyWeight pattern.

Essentially, separate state and functionality, bring the functionality to the state.

In array store only state.
> Hi, Alf
>
> So I can implement CFoo like:
>
> class FuncCFoo {
> public:
> virtual void work();
> };
>
> class CFoo {
> int m_data;
> //... all the state data
> static FuncCFoo* ptr; // pointer points to FuncCFoo which is a base
> class of CFoo functionality
> public:
> void work() {
> ptr->work();
> }
> };
>
> In this case, I only add 8 bytes. Any comments?

First, please don't top-post -- see the FAQ.

Regarding the technical, I'm not sure what the above is meant to achieve. On the
face of it the "static" pointer seems to mean that you want a polymorphic
instance of FuncCFoo associated with the /type/ CFoo. I don't understand why.

Anyways the 'ptr->work()' call would have to pass a reference to the state.

Consider perhaps something simpler like a very strong known-at-compile time
association between state and functionality classes,

class Foo
{
public:
class State
{
};

virtual void work( State& aState ) const {}
};

class Bar: public Foo
{
public:
virtual void work( State& aState ) const {}
};

int main()
{
std::vector<Foo::State> v;

v.push_back( Foo::State() );
for( size_t i = 0; i < v.size(); ++i ) { Bar().work( v.at( i ) ); }
}

I've not focused very much on type constraints here since I don't know what
constraints you'd want, but presumably there is a class corresponding to "Bar"
(otherwise the virtual routine wouldn't be an issue), and presumably it doesn't
add state or add anything to the class invariant, just a specialization of the
processing?


Cheers & hth.,

- Alf
 
D

DerTopper

Dear C++ Users,

I was facing a challenge in my application: we have a class, say,
CFoo, with virtual function. We need create a large a array of CFoo in
the application, CFoo array[10000]. Because of vptr of CFoo, we ended
up with 80000 bytes memory to store vptrs.

If the vtable adds 80000 bytes to your array size, you're probably
working on a 64-bit machine, so you're probably not developing for any
embedded stuff. We exactly are you worrying about 80000 bytes?
Is there a better way in
design or technically to save memory while keeping the ploymorphism? I
can only think of using function pointer instead.

I'd guess that using function pointers won't get the size of the array
smaller, since a function pointer will be as large as the vtable
pointer. Polymorphism and efficiency don't go very well together (at
least if we are talking about optimizing 80000 bytes), so you should
rather consider keeping separate arrays with non-polymorph objects.

Regards,
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,789
Messages
2,569,634
Members
45,342
Latest member
Sicuro

Latest Threads

Top