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...
Creating a stack template
Moderator: Coders of Rage
- Sanshin77
- 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
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
My YouTube Channel: http://www.youtube.com/user/Zanchill
-
- Chaos Rift Newbie
- Posts: 20
- Joined: Fri Mar 20, 2009 4:37 am
- Location: Germany
Re: Creating a stack template
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
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
- Sanshin77
- 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
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.
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
My YouTube Channel: http://www.youtube.com/user/Zanchill
- dandymcgee
- 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
http://www.codeproject.com/KB/cpp/Memor ... n_CPP.aspxSanshin77 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.
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!