Segfault在递归函数中

时间:2012-03-13 00:59:24

标签: c++ recursion segmentation-fault

我运行此代码时遇到段错误,我不确定原因。注释掉一个特定的行(标记如下)会删除段错误,这使我相信迭代器“i”的递归使用可能会造成麻烦,但即使将其更改为指针,我也会遇到段错误。

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”是链接列表的链接列表,我必须在递归期间发送给函数,声明如下:

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


    executeCommands(stdinstring,trsMetastack);

“Getallarguments”函数只是为了提取给定字符串的大部分字符串,如下所示:

    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);
    }

这是链表类定义。

    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; 
    };

感谢您花时间阅读本文。我知道这个问题很模糊,但我花了最后一小时尝试使用gdb进行调试,老实说,我不知道它可能是什么。

1 个答案:

答案 0 :(得分:1)

它可能是任何东西,但我的猜测是,讽刺的是:堆栈溢出。 您可能希望尝试将数据结构作为引用传递,例如:

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

但正如弗拉德指出的那样,你可能想熟悉gdb。