簡體   English   中英

在這種情況下是什么導致 zsh:segmentation 錯誤?

[英]What is causing zsh:segmentation fault in this case?

這是我第一次在 stackoverflow 上發布問題,所以如果我的問題看起來很混亂,我會事先道歉。 我有一個名為 class 的令牌,以及從它派生的 class,名為 function、Z157DB7DF5300235755185D366 運算符。 Token 有一個 int 類型的公共變量,稱為 _type。 我為每個派生類分配了不同的 _type 值。 integer 的類型為 1,運算符為 2,function 為 3。 這樣做的目的是讓我可以根據遇到的類型實現不同的操作。 我做了一個 rpn function,里面有這樣的東西。

double RPN::rpn(){
     
    Stack<Token*> rpn_stack;

    while(input_q.empty() == false){

        if(input_q.front()->_type == 1){

            rpn_stack.push(input_q.pop());
        }

        if(input_q.front()->_type == 2){

            double x = rpn_stack.pop()->value();
            double y = rpn_stack.pop()->value();

            //cout << x << y;

            char i = input_q.pop()->op();

            if(i == '*'){

                double result;
                result = x * y;
                rpn_stack.push(new Integer(result));

            }
        }

        if(input_q.front()->_type == 3){

            input_q.pop();
            rpn_stack.push(new Integer(_val));
           
        }
     }
     return rpn_stack.top()->value();
}

經過幾次測試,我發現 zsh:segmentation fault 問題是由 input_q.pop() 引起的

        if(input_q.front()->_type == 3){

            input_q.pop();
            rpn_stack.push(new Integer(_val));
           
        }

但是,如果我像這樣更改 if 語句的順序,則代碼可以工作

double RPN::rpn(){
     
    Stack<Token*> rpn_stack;

    while(input_q.empty() == false){

        if(input_q.front()->_type == 3){

            input_q.pop();
            rpn_stack.push(new Integer(_val));
           
        }

        if(input_q.front()->_type == 1){

            rpn_stack.push(input_q.pop());
        }

        if(input_q.front()->_type == 2){

            double x = rpn_stack.pop()->value();
            double y = rpn_stack.pop()->value();

            //cout << x << y;

            char i = input_q.pop()->op();

            if(i == '*'){

                double result;
                result = x * y;
                rpn_stack.push(new Integer(result));

            }
        }
     }
     return rpn_stack.top()->value();
}

這讓我懷疑問題是否首先是 input_q.pop() 。 如果我要像這樣在 while 循環內將 input_q.front()->_type 分配給 static 變量,它也可以工作

double RPN::rpn(){
     
    Stack<Token*> rpn_stack;

    while(input_q.empty() == false){

        int type = input_q.front()->_type;

        if(type == 1){

            rpn_stack.push(input_q.pop());
        }

        if(type == 2){

            double x = rpn_stack.pop()->value();
            double y = rpn_stack.pop()->value();

            //cout << x << y;

            char i = input_q.pop()->op();

            if(i == '*'){

                double result;
                result = x * y;
                rpn_stack.push(new Integer(result));

            }
        }

        if(type == 3){

            input_q.pop();
            rpn_stack.push(new Integer(_val));
           
        }
       
    }
    return rpn_stack.top()->value();
}

在這一點上,我對使用第一個版本時導致 zsh:segmentation 錯誤的原因感到非常困惑。 我想知道問題是否在於訪問 input_q.front()->_type? 我可以只使用第二個和第三個版本,但我真的很想知道是什么導致了第一個版本的 zsh:segmentation 錯誤。 如果有幫助,這是我編寫的用於測試它的簡單程序。 如果有幫助,我也在使用堆棧和隊列。

    Queue<Token *> postfix;
    postfix.push(new Integer(3));
    postfix.push(new Integer(5));
    postfix.push(new Operator("*"));
    RPN rpn(postfix);
    cout << "3 5 *: " << rpn() << endl;
    cout << "-------------" << endl;
    Queue<Token *> postfix2;
    postfix2.push(new Integer(3));
    postfix2.push(new Function("X"));
    postfix2.push(new Operator("*"));
    rpn.set_input(postfix2);
    cout << "3 x *: (x=3): " << rpn(3) << endl;

使用 RPN class 中的 () 運算符調用 rpn function。 RPN class 有一個名為 _val 的私有變量,由 () 運算符分配一個值。 希望我提供的信息足夠了。 感謝您的寶貴時間,不勝感激!

參考您的第一個代碼片段,當您這樣說時:

if(input_q.front()->_type == 1){
    rpn_stack.push(input_q.pop());
}

if(input_q.front()->_type == 2){
...

第二次調用input_q.front時,您不會檢查輸入隊列中是否還有任何內容。

一個簡單的解決方法是插入一條continue語句,如下所示:

if(input_q.front()->_type == 1){
    rpn_stack.push(input_q.pop());
    continue;
}

...

然后,您將循環回到while語句,該語句在繼續之前檢查隊列是否為空。

代碼中還有其他地方需要進行類似的更改,但你明白了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM