Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Architecture practice

This thread is locked; no one can reply to it. rss feed Print
Architecture practice
Travis Peterson
Member #16,756
October 2017

What are the pros/cons of making an allegro game using primarily vectors and objects declared on the stack:

std::vector<player> playerVector;
player Player;
playerVector.push_back(Player);

Vs vectors of pointers similar to this method:

std::vector<player*> playerVector;
playerVector.push_back(new Player);

Is the second method much faster? Preciate any knowledge yall can share

beoran
Member #12,636
March 2011

If it is a small game using the "stack", it will be ok, but with many objects you risk running out of space and smashing the stack. If you don't want to do dynamic memory management, then a good old static array of struct might be good enough.

Audric
Member #907
January 2001

Even if you store a vector on the stack, it only occupies a constant 12 bytes (*) sizeof(vector), which stores the maintenance data. You can assume it keeps track of size of an element, allocated count, used count, and pointer to the first one.

But the actual memory block is always allocated on the heap, because it is the only way for it to be freed when you need the vector to grow. If you think about it, space on the stack can never be allocated / freed.

(*) edited, I misread a stackoverflow page
https://stackoverflow.com/questions/17472940/sizeof-on-c-standard-library

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Something you want to be aware of is object copying. When you create a vector of objects (not pointers) then they are subject to copying at anytime. If you create a vector of pointers, then copying them does nothing.

It depends on what you want to use the data for. Do you need contiguous memory? Or you worried about the cache?

Do you need polymorphism? Then you need pointers.

Do you need memory management? Then I would recommend std::shared_ptr (C++ 11 Thank god).

Go to: