|
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; Vs vectors of pointers similar to this method: std::vector<player*> playerVector; 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 |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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). My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|