Creating a stack template

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Creating a stack template

Post by Sanshin77 »

After finishing up my book on C++ I started creating some containers(templates) Ive done a simple Array, and a linkedlist so far and now I wanna make a stack. Ive been looking around and wikipedia mentions implementing a stack with either a linkedlist or an array. Both seem pretty simple and I think I'd go for a linkedlist. I'm just wondering if there are any other preferable solutions, as fast as possible, because the future uses would involve a lot of pushes and pop's per frame.

What's the best/most common/fastest way? What kind of solution does hardcore dudes like the people behind OpenGL use for glPush- and glPop- Matrix ?

Thanks in advance...

Edit: Seems noone wants to answer...
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: Creating a stack template

Post by fantastico »

You have to think about how you will use the stack and then choose the implementation to squeeze out the last bit of performance if you really need that, in terms of asymptotic complexity a vector and a doubly-linked list are equal.
However, a list will only hold the objects that are actually on the stack currently, while a vector usually won't shrink again if you pop items from the stack. On the other side, the overhead of pushing onto a vector is less than appending to a list, especially if you know in advance how many items there will be at most and initialize the vector with a large enough capacity so resizing operations will be rare.
So you have a tradeoff between wasted memory and slighty better performance in some cases, I suggest you implement both, test and tell us :-)
User avatar
Sanshin77
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Tue Mar 10, 2009 9:36 am
Current Project: C++/SDL engine, zaActionWizardMagic game
Favorite Gaming Platforms: Xbox 360, Playstation 2, Nintendo DS, mac and PC
Programming Language of Choice: C++

Re: Creating a stack template

Post by Sanshin77 »

Im not good at testing tho, tried using instruments(mac app) to test for memory leaks with my linkedlist, didn't understand a shit :/
Ive started on the linkedlist implementation so we'll see how that goes.
Check out videos of my C++ games as well as my "Amateur Game Dev" series over at
My YouTube Channel: http://www.youtube.com/user/Zanchill
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Creating a stack template

Post by dandymcgee »

Sanshin77 wrote:Im not good at testing tho, tried using instruments(mac app) to test for memory leaks with my linkedlist, didn't understand a shit :/
Ive started on the linkedlist implementation so we'll see how that goes.
http://www.codeproject.com/KB/cpp/Memor ... n_CPP.aspx

You might wanna check this out. Personally, I've not used any of these concepts in any of my projects but I found it to be an interesting read.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply