tags:

views:

197

answers:

5

Can C++ std::stack handle more than 10k int items? And how about its performance?

+8  A: 

10K what? Most std::stack implementations could easily handle 10K primitives with good performance, since that's probably < 80KB. However, the standard doesn't guarantee performance.

Also note that it depends what backend you use. The default is std::deque, which is basically an array of arrays. However, you should get decent performance with any backend.

Matthew Flaschen
std::deque is most commonly implemented as an array of arrays. A list of arrays would not meet the complexity requirements set forth in the standard.
CTT
Thanks @CTT, clarified now.
Matthew Flaschen
+5  A: 

A std::stack is only limited by the underlying container (a std::stack is a container adapter).

Depending on what the underlying container is, you'll have different limits.

A std::vector will require contiguous memory whereas a std::list will only be limited by the size of the heap (and possibly any heap fragmentation). But std::deque (the default container) is between the two; it requires smaller chunks of contiguous memory but will primarily be limited by the size of the heap.

R Samuel Klatchko
+2  A: 

Yes, it can handle 10k. As long as you have the memory resources for it. If you are worried it uses the internal stack, it doesn't.

Performance is implementation specific, but should be very quick

Keith Nicholas
So I can't get stack overflow right?
Quang Anh
A stack overflow is a little confusing in this context... If you mean that your std::stack may overflow, then yes, it's possible, but that would mean your system has completely run out of memory, which is unlikely given the 10K figure. As for an overflow of the execution stack, that would not apply in this case as you are dealing with dynamic memory.
BennyG
+1  A: 

std::stack is not a container, but a container adapter. By default it uses an std::deque as container, but you can also use a std::vector or std::list. Deques free their memory when elements are removed.

Eddy Pronk
The default container is `std::deque`
R Samuel Klatchko
+1  A: 

The performance depends on the underlying container used. As already mentioned, stack is an adapter, the underlying container can be deque (the default), or vector, or list (all in std namespace).

Following is an example of performance comparison. As the type to be stored is not clearly mentioned in the question, I am assuming it to be unsigned int. Feel free to change it per your requirements. The example constructs a stack of 100K items.

Contents of stack.cpp

#include <stack>
#include <vector>
#include <list>
#include <deque>
#include <assert.h>

typedef unsigned int Type;

#ifdef USE_VECTOR
typedef std::stack< Type, std::vector< Type > > StackType;
#elif USE_LIST
typedef std::stack< Type, std::list< Type > > StackType;
#else
typedef std::stack< Type, std::deque< Type > > StackType;
#endif

int main() {
    StackType mystack;
    for( unsigned int i = 0; i < 100 * 1024; ++i ) {
        mystack.push( i );
    }
    assert( mystack.size() == 100 * 1024 );
    return 0;
}

Execution comparison:

$ g++ -DUSE_VECTOR stack.cpp -o stack; time ./stack

real    0m0.023s
user    0m0.030s
sys     0m0.031s

$ g++ -DUSE_LIST stack.cpp -o stack; time ./stack

real    0m0.144s
user    0m0.186s
sys     0m0.030s

$ g++  stack.cpp -o stack; time ./stack

real    0m0.024s
user    0m0.030s
sys     0m0.015s

asaha@nedata8 ~/code
$ 

The above numbers are result of single run. To achieve statistically significant numbers run each variation large number of times, and observe the mean and standard deviation.

Apparently, deque and vector results in similar performance, whereas list is worse.

ArunSaha
Technically, those are the only standard containers that can be used in a `stack`, but it also supports custom sequences provided they expose the appropriate interface. I think `back()` `push_back()` and `pop_back()` are the required functions, but I don't remember off hand.
Dennis Zickefoose
@Dennis: Yes, custom sequences are okay, as long as they support `back()`, `push_back()`, and `pop_front()`. Source: http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00693.html#_details
ArunSaha