【问题标题】:Runtime error SIGSEGV infix to postfix运行时错误 SIGSEGV 中缀到后缀
【发布时间】:2014-09-18 12:32:59
【问题描述】:

这段代码在我的机器上运行良好,但是当我将它上传到 codechef 时,它给了我一个运行时错误 SIGSEGV。谁能指出我的代码中的错误?这是我为http://www.codechef.com/problems/ONP/提出的问题

#include<iostream>  
#include<string>  
using namespace std;  
class stack  
{  
    public:  
        void push(char a)  
        {  
            ++top;    
            arr[top]=a;  
        }  
        void pop()  
        {  
            top--;  
        }  
        void initialize(int size)  
        {  
            top=-1;  
            max=size;  
        }  
        bool chckfull()  
        {  
            return (top==max-1);  
        }  
        bool chckempty()  
        {  
            return (top==-1);  
        }  
        char front()  
        {  
            return arr[top];  
        }  
        private:  
            int top;  
            int max;  
            char arr[404];  
};  
int chckalphanum(char y)  
{  
    if((y>='a')&&(y<='z'))  
    return 1;  
    else if ((y>='A')&&(y<'Z'))  
    return 1;  
    else if((y>='0')&&(y<='9'))  
    return 1;  
    return 0;  
}  
int pre(char x)  
{  
    if(chckalphanum(x))  
    return 0;  
    if(x=='(')  
    return -1;  
    else if(x=='^')  
    return 3;  
    else if((x=='/')||(x=='*'))  
    return 2;  
    else  
    return 1;  
}  
int main ()  
{  
    std::ios::sync_with_stdio(false);  
    string s, s1=")";  
    char q[404];  
    int qmax=0,t;  
    stack prs;  
    scanf("%d", &t);  
    while(t--)  
    {  
    cin>>s;  
    prs.initialize(s.length());  
    prs.push('(');  
    s=s+s1;  
    for(int i=0; i<s.length(); i++)  
    {  
        if(s[i]=='(')  
        prs.push('(');  
        else if(chckalphanum(s[i]))  
        {  
            q[qmax]=s[i];  
            qmax++;  
        }  
        else if(s[i]==')')  
        {  
            while(prs.front()!='(')  
            {  
                q[qmax]=prs.front();  
                qmax++;  
                prs.pop();  
            }  
            prs.pop();  
        }  
        else  
        {  
             while(pre(prs.front())>=pre(s[i]))  
             {  
                q[qmax]=prs.front();  
                qmax++;  
                prs.pop();  
             }  
             prs.push(s[i]);  
        }  
    }  
    for(int i=0; i<qmax; i++)  
    cout<<q[i];  
    cout<<"\n";  
    qmax=0;  
    }  
return 0;  
}

【问题讨论】:

  • 我愿意。我写了一个初始化函数。并将顶部初始化为 -1。
  • 我也调用了那个函数。就在我输入字符串之后。
  • 是的,我现在明白了!让我检查错误。!非常感谢您抽出宝贵的时间 :) 它的代码很大
  • 除非你以某种方式达到极限(说真的,你的堆栈应该做一些边界检查,你也可以使用at() 将数组替换为向量来检查),我看不到任何立即错误
  • @PranjalRanjan 边界检查是不必要的,前提是您可以确定您永远不会创建包含错误的代码。你至少应该确保你不会推入一个完整的堆栈或弹出一个空的堆栈。

标签: c++ runtime iostream segmentation-fault


【解决方案1】:

我刚刚从您的解决方案中注释掉了以下行,它被 codechef 接受了。

std::ios::sync_with_stdio(false);

我不确定您是否知道上述行的作用,但我会尽我所能解释。社区一定会在适当的时候提供更好的答案。

“关闭 stdio 同步后,iostream 标准流对象可以独立于标准 C 流进行操作(尽管它们不是必需的),并且混合操作可能会导致意外的交错字符。”

引用 cppreference。

“对同一流对象的并发访问可能会导致数据争用。”

由于您已关闭 stdio(C 风格 I/O)和 iostream(C++ 风格 I/O)之间的同步 你继续使用 scanf cin 同时交错,我怀疑你遇到了运行时错误。

更多研究请通过:http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/

希望它能澄清一点,如果不是完全的话。谢谢!

【讨论】:

  • 谢谢你..!你是绝对正确的!非常感谢! :-)
猜你喜欢
  • 2021-04-28
  • 1970-01-01
  • 2012-01-05
  • 2012-12-20
  • 2015-06-14
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
相关资源
最近更新 更多