0

I am following the book Data Structures Using C++, 2nd Edition, D.S Malik and in the book a stack(ADT) is implemented as an array (page 400). I followed the code exactly but when I try to call .push() the program crashes; all other member functions execute without error. Any idea why it is crashing? Code below:

The abstract class:

#include <iostream>

template <class T>
class stackADT
{
public:
    virtual void initialiseStack() = 0;
    virtual bool isEmpty() const = 0;
    virtual bool isFull() const = 0;
    virtual void push(const T& newItem) = 0;
    virtual T top() const = 0;
    virtual void pop() = 0;
}; 

StackType class definition: (in the book definitions and implementation are placed in one .h file)

#ifndef STACKTYPE_H
#define STACKTYPE_H

#include <iostream>
#include <cassert>

#include "StackADT.h"

using namespace std;

template <class T>
class stackType: public stackADT<T>
{
public:
    const stackType<T>& operator=(const stackType&); //
    void initialiseStack(); // initialises stack to size of 100
    bool isEmpty() const;
    bool isFull() const;
    void push(const T& newItem);
    T top() const;
    void pop();
    stackType(int stackSize = 100);
    stackType(const stackType<T>& otherStack);
    ~stackType();
    void update(T t);
    int getStackTop() const;
private:
    int maxStackSize; //holds max num of elements in stack
    int stackTop;    //holds num of elements in stack
    T *list;        //pointer to the array holding stack elements
    void copyStack(const stackType<T>& otherStack);
};

StackType Implementation:

template <class T>
void stackType<T>::initialiseStack()
{
   stackTop = 100;
}

template <class T>
bool stackType<T>::isEmpty() const
{
    return(stackTop == 0);
}

template <class T>
bool stackType<T>::isFull() const
{
    return(stackTop == maxStackSize);
}

template <class T>
void stackType<T>::push(const T& item)
{
    if(!isFull())
    {
       list[stackTop] = item;
       stackTop++;
    }
    else
        cout << "Stack is full." << endl;
}

template <class T>
T stackType<T>::top() const
{
    assert(stackTop != 0);

    return (list[stackTop - 1]);
}

template <class T>
void stackType<T>::pop()
{
    stackTop--;
}

template <class T>
void stackType<T>::copyStack(const stackType<T>& otherStack)
{
    delete [] list;
    maxStackSize = otherStack.maxStackSize;
    stackTop = otherStack.stackTop;

    list = new T[maxStackSize];

    for(int i = 0; i < stackTop; i++)
    {
        list[i] = otherStack.list[i];
    }
}

template <class T>
stackType<T>::stackType(int stackSize)
{
    if(stackSize <= 0)
    {
        cout << "Size must be positive. Creating array of size 100" << endl;
        maxStackSize = 100;
    }
    else
    {
        maxStackSize = stackSize;
    }
}

template <class T>
stackType<T>::~stackType()
{
    delete [] list;
}

template <class T>
stackType<T>::stackType(const stackType<T>& otherStack)
{
    list = NULL;

    copyStack(otherStack);
}


template <class T>
void stackType<T>::update(T t)
{
    stackType tempStack(5);
    int i = 0;
    T itemFound;

    while(!isEmpty())
    {
        if(top() != t)
        {
            tempStack.list[i] = top();
            pop();
        }
        else
        {
            itemFound = stackTop - 1;
            pop();
        }
        i++;
    }

    i = 0;

    while( i < 4)
    {
        list[i] = tempStack.top();
        tempStack.pop();
        i++;
    }

    this->push(itemFound);

}

template <class T>
int stackType<T>::getStackTop() const
{
    return stackTop;
}

template <class T>
const stackType<T>& stackType<T>::operator=(const stackType<T>& otherStack)
{
    if(this != &otherStack)
        copyStack(otherStack);

    return *this;
}

#endif // STACKTYPE_H    
5
  • 1
    What is a "crash"? Post your exception type, message and stack trace Commented Aug 17, 2015 at 9:16
  • 1
    Where is implementation of 'initialiseStack'. The one you wrote here is just virtual function. Plus, where do you initialize other variables? Is that all of the code? If yes, then you will have multiple null pointers..! Commented Aug 17, 2015 at 9:18
  • 3
    @Epig how did you initialize list? Commented Aug 17, 2015 at 9:18
  • How does it crash? throws uncaught exception? Trigger CPU fault? Can you run it in a debugger to see where it crashes? Commented Aug 17, 2015 at 9:21
  • 1
    Other than that, how is list initialized? Is enough memory reserved for it? If it is NULL it would crash at list[stackTop] = newItem, but no other method should look in the array before first push has completed. Commented Aug 17, 2015 at 9:25

1 Answer 1

2

list doesn't seem to be initialized - resulting in a NULL pointer. The constructor should be something like:

template <class T>
stackType<T>::stackType(int stackSize)
{
    if(stackSize <= 0)
    {
        cout << "Size must be positive. Creating array of size 100" << endl;
        maxStackSize = 100;
    }
    else
    {
        maxStackSize = stackSize;
    }

    list = new T[maxStackSize];  // <<<< initialize list
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @skyking, this is what I was missing. I added the list initialisation and the program had no issues. I apologise for a seemingly dumb question but I am still learning. Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.