开发者

Advantage/Disadvantage of function pointers

So the problem I am having has not actually happened yet. I am planning out some code for a game I am currently working on and I know that I am going to be needing to conserve memory usage as much as possible from step one.

My question is, if I have for example, 500k objects that will need to constantly be constructed and deconstructed. Would is save me any memory to have the functions those classes are going to use as function pointers.

e.g. without function pointers

class MyClass

{

public:

void Update();

void Draw();

...

}

e.g. with function pointers

clas开发者_高级运维s MyClass

{

public:

void * Update;

void * Draw;

...

}

Would this save me any memory or would any new creation of MyClass just access the same functions that were defined for the rest of them? If it does save me any memory would it be enough to be worthwhile?


Assuming those are not virtual functions, you'd use more memory with function pointers.

The first example

There is no allocation (beyond the base amount required to make new return unique pointers, or your additional implementation that you ... ellipsized).

Non-virtual member functions are basically static functions taking a this pointer.

The advantage is that your objects are really simple, and you'll have only one place to look to find the corresponding code.

The disadvantage is that you lose some flexibility, and have to cram all your code into single update/draw functions. This will be hard to manage for anything but a tiny game.

The second example

You allocate 2x pointers per object instance. Pointers are usually 4x to 8x bytes each (depending on your target platform and your compiler).

The advantage you gain a lot of flexibility of being able to change the function you're pointing to at runtime, and can have a multitude of functions that implement it - thus supporting (but not guaranteeing) better code organization.

The disadvantage is that it will be harder to tell which function each instance will point to when you're debugging your application, or when you're simply reading through the code.

Other options

Using function pointers this specific way (instance data members) usually makes more sense in plain C, and less sense in C++.

If you want those functions to be bound at runtime, you may want to make them virtual instead. The cost is compiler/implementation dependent, but I believe it is (usually) going to be one v-table pointer per object instance.

Plus you can take full advantage of virtual function syntax to make your functions based on type, rather than whatever you bound them to. It will be easier to debug than the function pointer option, since you can simply look at the derived type to figure out what function a particular instance points to.

You also won't have to initialize those function pointers - the C++ type system would do the equivalent initialization automatically (by building the v-table, and initializing each instance's v-table pointer).

See: http://www.parashift.com/c++-faq-lite/virtual-functions.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜