0

My assignment is to implement a stack (array-based) with a given capacity that, when attempting to add another element after the stack is full, will grow by a constant value (I used 100).

The problem here I believe lies in my push() function, which adds 100 element to the stack... probably syntactical but I'm not sure at all why my program won't execute.

   template<class Type>
   class ArrayStack{
   enum {default_cap=100};
   private:
        Type* S; //array storing elements
        int CAP; //capacity of stack
        int TOP; //top element of stack
   public:
        ArrayStack(int defc = default_cap); //constructor with default parameter
        ~ArrayStack(){} //is "delete [] S;" supposed to go in here? not sure
        bool isEmpty() const { return (TOP<0); }//is the stack empty?
        int size() const { return (TOP+1); }
        const Type& top(){ return S[TOP];} //has exception handling, not displayed
        Type pop() {--TOP;} //removes top element

        //here's the function that concerns me:
   //--------------------------------------------
        void push (const Type& e){
        if(size() == CAP) {
            Type* Snew = new Type[CAP+100];
            for(int i = 0; i < CAP; i++){
                Snew[i] = S[i];
            }
            delete [] S;
            ++CAP;
            S = Snew;
       }
        S[++TOP] = e;
    }
   //--------------------------------------------
   //other functions...
   };

   //constructor:
   template<typename T> ArrayStack<T>::ArrayStack(int d)
   :    S(new T[d]), CAP(d), TOP(-1){}
6
  • Can you give us more information about "won't execute"?
    – arc_lupus
    Jan 31, 2015 at 7:34
  • I'm sorry. On CodeBlocks the console window just stays black (with the blinking "_") and on ideone.com it just says "Running" for a few seconds then it times out. @arc_lupus
    – user11892
    Jan 31, 2015 at 7:35
  • Then at least I need the minimal running code, otherwise I have no possibility to test it on my own.
    – arc_lupus
    Jan 31, 2015 at 7:37
  • 3
    Just reading that push function, it seems that whenever you exceed your capacity, you increase it by 100 but only increment CAP by one. So you'll be reallocating that array on every push past 100. You need to show more code. Jan 31, 2015 at 7:42
  • In addition to what @RogerRowland said, you need to show us how are you using this class.
    – Mido
    Jan 31, 2015 at 8:02

1 Answer 1

0

It's a bit hard to comment since you've only provided partial code, and haven't demonstrated usage (e.g. with a main() function).

However, an obvious problem [which I notice Roger Rowland has identified in his comment too] with the push() function is that it increases allocated size by 100, but only increments CAP. So it will add 100 elements to the array, but only report ability to use the first one added.

The pop() function also discards the top element, and doesn't return it. If the caller ever tries to use the return value from pop() - and users of a stack type do normally expect to be able to use values they pop - the result will be undefined behaviour.

Your destructor definitely needs to use operator delete, unless you clean up dynamically allocated memory in some other way (and you've shown nothing like that). The whole point of operator new is that memory is NOT released until a corresponding operator delete. It will not be cleaned up magically if you forget to do it, and will present a memory leak for (at least) as long as your program runs.

If you want to do things a bit more safely, use a std::vector instead of a pointer (and avoid using operator new directly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.