xyzzy
xyzzy

Reputation: 23

Segfault in recursive function

I'm getting a segfault when I run this code and I'm not sure why. Commenting out a particular line (marked below) removes the segfault, which led me to believe that the recursive use of the iterator "i" may have been causing trouble, but even after changing it to a pointer I get a segfault.

void executeCommands(string inputstream, linklist<linklist<transform> > trsMetastack)
{
int * i=new int;
(*i) = 0;
while((*i)<inputstream.length())
{
    string command = getCommand((*i),inputstream);
    string cmd = getArguments(command,0);
    //cout << getArguments(command,0) << " " << endl;


    if (cmd=="translate")
    {

        transform trs;
        trs.type=1;
        trs.arguments[0]=getValue(getArguments(command,2));
        trs.arguments[1]=getValue(getArguments(command,3));
        ((trsMetastack.top)->value).push(trs);
        executeCommands(getArguments(command,1),trsMetastack);
    }

    if (cmd=="group")
    { 
        //make a NEW TRANSFORMS STACK, set CURRENT stack to that one
        linklist<transform> transformStack;
        trsMetastack.push(transformStack);


        //cout << "|" << getAllArguments(command) << "|" << endl;
        executeCommands(getAllArguments(command),trsMetastack); // COMMENTING THIS LINE OUT removes the segfault

    }

    if (cmd=="line")
    { //POP transforms off of the whole stack/metastack conglomeration and apply them.


        while ((trsMetastack.isEmpty())==0)
        {
            while ((((trsMetastack.top)->value).isEmpty())==0)   //this pops a single _stack_ in the metastack
            { transform tBA = ((trsMetastack.top)->value).pop();
                cout << tBA.type << tBA.arguments[0] << tBA.arguments[1];
            }
            trsMetastack.pop();
        }


    }

"Metastack" is a linked list of linked lists that I have to send to the function during recursion, declared as such:

    linklist<transform> transformStack;
    linklist<linklist<transform> > trsMetastack;
    trsMetastack.push(transformStack);


    executeCommands(stdinstring,trsMetastack);

The "Getallarguments" function is just meant to extract a majority of a string given it, like so:

    string getAllArguments(string expr) // Gets the whole string of arguments
    {
        expr = expr.replace(0,1," "); 
        int space = expr.find_first_of(" ",1);
        return expr.substr(space+1,expr.length()-space-1);
    }

And here is the linked list class definition.

    template <class dataclass> 
    struct linkm {
        dataclass value;     //transform object, point object, string... you name it
        linkm *next;
    };

    template <class dataclass> 
    class linklist 
    {
    public:
        linklist()
        {top = NULL;}
        ~linklist() 
        {}
        void push(dataclass num)
        {
            cout << "pushed";
            linkm<dataclass> *temp = new linkm<dataclass>;
            temp->value = num;
            temp->next = top;
            top = temp;
        }   
        dataclass pop()
        {

            cout << "pop"<< endl;
            //if (top == NULL) {return dataclass obj;}
            linkm<dataclass> * temp;
            temp = top;
            dataclass value;
            value = temp->value;
            top = temp->next;
            delete temp;
            return value;
        }
        bool isEmpty()
        {
            if (top == NULL) 
            return 1;
            return 0;
        }
        //  private:
        linkm<dataclass> *top; 
    };

Thanks for taking the time to read this. I know the problem is vague but I just spent the last hour trying to debug this with gdb, I honestly dunno what it could be.

Upvotes: 2

Views: 731

Answers (1)

Daniel Roethlisberger
Daniel Roethlisberger

Reputation: 7058

It could be anything, but my wild guess is, ironically: stack overflow. You might want to try passing your data structures around as references, e.g.:

void executeCommands(string &inputstream, linklist<linklist<transform> > &trsMetastack)

But as Vlad has pointed out, you might want to get familiar with gdb.

Upvotes: 1

Related Questions