Hey all. I've made stacks aplenty in Java before; my C++ is a bit rusty though, so I thought this would be a good project for me. Turns out I was rustier than I thought!

So here's the situation: I've made classes LinkedNode, jStack, and Test. Test simply holds an integer value, and its showVal() function prints it to the screen with a newline. LinkedNode should hold a pointer to some generic object (in my case, Test) and a pointer to another LinkedNode of the same type.

Here is LinkedNode, for reference:

template <class T>
class LinkedNode{
  private:
    T* element;
    LinkedNode<T>* link;
    bool hasElementFlag;
    bool hasLinkFlag;
  public:
    LinkedNode(){
      element = NULL;
      link = NULL;
      hasElementFlag = false;
      hasLinkFlag = false;
    }
    LinkedNode(T* elementPtr){
      element = elementPtr;
      link = NULL;
      hasElementFlag = true;
      hasLinkFlag = false;
    }
    LinkedNode(T* elementPtr, LinkedNode<T>* linkPtr){
      element = elementPtr;
      link = linkPtr;
      hasElementFlag = true;
      hasLinkFlag = false;
    }
    bool hasElement(){
      return hasElementFlag;
    }
    T* getElement(){
      if(hasElementFlag) return element;
      return NULL;
    }
    void setElement(T* elementPtr){
      element = elementPtr;
      hasElementFlag = true;
    }
    bool hasLink(){
      return hasLinkFlag;
    }
    LinkedNode<T>* getLink(){
      if(hasLinkFlag) return link;
      return NULL;
    }
    void setLink(LinkedNode<T>* linkPtr){
      link = linkPtr;
      hasLinkFlag = true;
    }
    void severLink(){
      link = NULL;
      hasLinkFlag = false;
    }
};

jStack should hold a pointer to the top element of a stack (a LinkedNode) and an integer counter, and do all the fun stacky things a good stack does. Here is jStack:

template <class T>
class jStack{
  private:
    LinkedNode<T>* top;
    int count;
  public:
    jStack<T>(){
      top = NULL;
      count = 0;
    }
    jStack<T>(T* element){
      LinkedNode<T> node(element);
      top = &node;
      count = 1;
    }
    int size(){
      return count;
    }
    void push(T* element){
      LinkedNode<T> node(element);
      if(count == 0) top = &node;
      else{
        node.setLink(top);
        top = &node;
      }
      count++;
    }
    T* pop(){
      //(top->getElement())->showVal();
      if(count == 0) return NULL;
      T* element = top->getElement();
      if(count > 1) top = top->getLink();
      count--;
    }
    void peek(){
      (top->getElement())->showVal();
    }
};

My main program is still just a simple test of jStack. I create a Test object that holds the value 10, create a stack, push the pointer to the Test object onto the stack. Then, I pop the pointer back off and into a pointer of type Test*.

Theoretically, when I call the showVal() method from the pointer x, I should see '10', but instead I see some random huge negative number. Am I accessing the value of the underlying element (the Test inside the LinkedNode at the top of the jStack) incorrectly, or am I just going insane?

Here is the main program:

int main(void){
  
  Test a(10);

  // This prints out '10' like it should, so '10' is definitely in there!
  a.showVal();  

  jStack<Test> s;
  s.push(&a);
  
  Test* x;
  x = s.pop();
  
  // This prints out a random huge number!
  x->showVal();
  
  system("PAUSE");
  return 0;
}

The output from this is:
-1017291941
which should be, theoretically:
10

Any help with this would be greatly appreciated. Perhaps I am being silly here, but this has been driving me nuts for a while now.

All the best, folks.
- jthechemist

Look at your class constructor:

jStack<T>(T* element) {
      LinkedNode<T> node(element);
      top = &node;
      count = 1;
}

Now what happens: you create LOCAL (in the stack) variable node and place its address (the pointer to local node var) in the top pointer (don't forget: a constructor is a function with local variables declared in its body). After that your program returns from the constructor so all its local variables are discarded (out of scope). Now the top pointer refers to nowhere (the stack memory reused by other calls).
That's why your program prints a garbage.
A robust stack class must create its own copies of stacked elements (by new operator, for example). It's too unsafe approach to stack pointers only: the stack object does not know stacked elements scope and duration.

This article has been dead for over six months. Start a new discussion instead.