Search code examples
c++graphstackcodeblocksterminate

c++ code works properly but the process ends with termination instead return 0


i wrote a simple c++ code in codeblocks and i implemented stack and graph classes for that with dynamic memory allocation. my code works properly and gives correct output but at the end it shows ***.exe has stopped working error and shows "Process terminated with status -1073741819" in build log. i tried GNU gdb 6.8 debugger and it couldn't find any errors.

this problem was made after imlementing stack class, so this is my code if it can helps solving problem:

class stack
{
    vertex* d;
    int end;
    public:
    stack()
    {
        end=0;
        d=NULL;
    }
    void create(int n)
    {
        d=new vertex[n];
    }
    vertex top()
    {
        return d[end];
    }
    void push(vertex y)
    {
        end++;
        d[end]=y;
    }
    vertex pop()
    {
        end--;
        return d[end+1];
    }
    ~stack()
    {
        if (d!=NULL)
            delete d;
    }
};
  • vertex class is also declared before stack.
  • for some inputs, debugger says "Program received signal SIGSEGV, Segmentation fault."

edit: main asked:

int main()
{
    G graf;
    graf.get();
    stack tree;
    tree.create(graf.q()-1);

    int q=0;
    int i=0;
    int u=0;

    while (u<graf.q()-1)
    {
        tree.push(graf.u[i]);
        if (graf.u[i].r[0]->flag > 0 && graf.u[i].r[1]->flag > 0 && u>=q)
            tree.pop();
        else
        {
            u++;
            if (graf.u[i].r[0]->flag==0)
                q++;
            if (graf.u[i].r[1]->flag==0)
                q++;
            graf.u[i].r[0]->flag++;
            graf.u[i].r[1]->flag++;
            cout << tree.top().r[0]->name << " - " << tree.top().r[1]->name << '\n';
        }
        i++;
    }
    return 0;
}
  • i even tried adding a cout just before return 0 and my text printed.

Solution

  • Your code is wrong:

    void push(vertex y)
    {
        end++;
        d[end]=y;
    }
    

    Should be:

    void push(vertex y)
    {
        d[end]=y;
        end++;
    }
    

    Else, first pushed item goes to position 1instead of position 0.

    Moreover, stack::top() returns next item, not last pushed:

    vertex top()
    {
        return d[end];
    }
    

    should be:

    vertex top()
    {
        return d[end-1];
    }
    

    I'm pretty sure you seg fault is due to unallocated memory being accessed, add assertions to have the program notify you when something gets wrong, like that:

    class stack
    {
        vertex* d;
        int cur; 
        int capacity;
        public:
        stack()
        {
            cur=0;
            capacity=0;
            d=NULL;
        }
        void create(int n)
        {
            assert( d == NULL );
            capacity = n;
            d=new vertex[n];
        }
        vertex top()
        {
            assert( cur != 0 );
            return d[cur-1];
        }
        void push(vertex y)
        {
            cur++;
            assert( cur < capacity );
            d[cur]=y;
        }
        vertex pop()
        {
            assert( cur > 0 );
            cur--;
            return d[cur+1];
        }
        ~stack()
        {
            if ( d != NULL )
                delete [] d;
        }
    };
    

    Then, run again, you'll see where you get an assertion.

    Finally, check vertex copy constructor works fine, because pushing/poping does a lot of vertexcopy, if there's something wrong here, it could cause seg fault.