0

Here is the code:

#include "DynIntStack.h"


DynIntStack::DynIntStack(void)
{
}


DynIntStack::~DynIntStack(void)
{
}

bool DynIntStack::IsEmpty()
{
    return head;
}
void DynIntStack::Push(int v)
{
    Element e = Element(v, head);
    head = &e;
}
int DynIntStack::Pop()
{
    if(head)
    {
        int r = head->v;
        head = head->next;
        return r;
    }
}
int DynIntStack::Top()
{
    if(head)
        return head->v;
}
string DynIntStack::Print()
{
    stringstream ss;
    ss << "IntStack {";
    Element *k = head;
    while (k)
    {
        ss << k->v << ", ";
        k = k->next;
    }
    ss << "}";
    return ss.str();
}

The "head" pointer seems to lose its value every time I call push. Why? Often times the head element will contain a pointer to ITSELF, which should not be possible with this code...

pixartist
  • 1,137
  • 2
  • 18
  • 40

2 Answers2

4

Because you create object on stack, when object goes out of scope it's destroyed. As a result head becomes dangling pointer, since it references freed memory.

void DynIntStack::Push(int v)
{
    Element e = Element(v, head);
    head = &e;
}

What you need to do is to allocate object on heap, something like this:

Element * e = new Element(v, head);
head = e;
nogard
  • 9,432
  • 6
  • 33
  • 53
  • Thanks, I did not know c++ had a garbage collector – pixartist May 14 '13 at 11:55
  • @user1071988: C++ does not have GC. But objects allocated on stack are destroyed when they go out of scope. – nogard May 14 '13 at 11:57
  • Okay, so when exactly are objects created on the stack ? – pixartist May 14 '13 at 12:16
  • @user1071988: When you don't allocate them explicitly on the heap with new or malloc. I propose to read [this nice discussion](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) about the subject. – nogard May 14 '13 at 12:21
  • Well, I don't see declaration of `head`, but I can conclude this is 100% pointer, because you: 1. assign pointer to it. 2. compare it with NULL (`if(head)`). It's clearly a pointer, you simply can't do this kind of things with non-pointer. – nogard May 14 '13 at 12:29
  • Where is the space for class members allocated ? My guess would be the heap, but can't I initialize them without malloc / new ? Couln't I change head to a non pointer and write head = Element(v, &head) ? – pixartist May 14 '13 at 12:34
  • I believe 'head' must be a pointer (or smart pointer), that's just fine for your needs. Comments are for requesting clarification, not for "follow-up questions". I suggest you to spend some time now learning the C++ basics and come back in a few days if you still can't work it out. – nogard May 14 '13 at 12:47
1

Your program exibits undefined behavior in Top(). When head == NULL, the function doesn't return anything - all gloves are off afterwards.

Same with Pop, which also appears to be implemented incorrectly. Running the code through a debugger would help.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625