Archived

This topic is now archived and is closed to further replies.

SpaceRogue

Speed: Pointers vs. Direct

Recommended Posts

I keep reading that multiple pointer derefencing is slow, i.e. a ''bad thing''. Is it really that much slower than directly accessing an object? In other words is: apointer->anotherpointer->memberfunction() that much slower than: myobject.memberfunction() ? How about with just a single dereference? I mean if I''m going to call a function several thousands of times, will pointer->func() be much slower than object.func() ?

Share this post


Link to post
Share on other sites
in most cases, yes it is slower, but it wont freeze the computer. the time that it takes to derefrence that is almost nothing, so defrerencing twice or three times would be alright, although confuzing. try to avoid object->object->object->object->object->object->object->object->object->object->function().

sometimes using pointers to pointers is necessary, like a player in an rpg having a backpack object, and having items inside the backpack.

player->backpack->item->use();

if you did player.backpack.item.use() it gets just as confusing, and it doesn''t allow to change backpacks that easily etc.

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

Share this post


Link to post
Share on other sites
If you are going into a loop that will be doing dereferencing a whole bunch (i.e. every time the loop iterates), you should just dereference once, store that value into a pointer, and then in your loop access the temporary pointer that contains the dereferenced value.

Share this post


Link to post
Share on other sites