休息;导致段故障 [英] break; causing segment fault

查看:81
本文介绍了休息;导致段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用堆栈进行括号检查.包含 break; 语句的 if else 语句之一导致段错误.

I was doing parenthesis checker using the stack. One of the if else statement containing a break; statement is causing segment fault.

我尝试删除break程序运行正常,但打印错误的答案,因为需要该break才能打印正确的输出.这种网段故障的​​原因是什么?中断无法访问任何存储单元.对吗?

I tried removing the break program runs fine but prints the wrong answer as that break is needed to print correct output. What is the cause of such a segment fault? the break doesn't access any memory unit.right?

问题链接

#include <iostream>
#include<stack>
using namespace std;

int main() {
//code
int n;
char c,comp;
cin>>n;
while(n--)
{
   stack<char>s;
   while(cin>>c)
   {
       if(c=='(' || c=='{'|| c=='[')
       s.push(c);
       else
       {
           comp=s.top();
           if(c==')' && comp=='(')
           s.pop();  
           else if(c==')' && comp!='(')
           {
               cout<<"not balanced"<<endl;
               break;  //this one, if i remove this no SIGSEGV
           }


           if(c=='}' && comp=='{')
            s.pop();
           else if(c=='}' && comp!='{')
           {
               cout<<"not balanced"<<endl;
               break;
           }


           if(c==']' && comp=='[')
           s.pop();
           else if(c==']' && comp!='[')
           {
               cout<<"not balanced"<<endl;
               break;
           }


       }

   }

       if(s.empty())
       cout<<"balanced"<<endl; 
}

return 0;
 }

推荐答案

所以,首先是关于 std的一些背景信息:堆栈 ,将在以后变得相关:

So, first some background information on std::stack that will become relevant later:

按合同编程的样式将是具有非空堆栈是调用pop的先决条件,而不满足其先决条件的方法调用会产生 undefined 结果.

请注意,当堆栈中有0个元素时,虽然找不到找不到表示 s.top()的来源,但我将假定它也很糟糕出于同样的原因.

Please note that while I couldn't find a source that said s.top() is bad when there are 0 elements in a stack, I'm going to assume it would also be bad for the same reason.

为什么那么重要?好吧,未定义的行为可以做任何事情,包括段错误.只要把它放在脑后.

Why is that important? Well, undefined behavior can do anything, including segfault. Just keep that in the back of your head.

因此,此代码就在这里:

So, this code right here:

comp=s.top();

如果遇到 s 为空的情况,而您又从 std :: cin 输入了其他信息,会发生什么情况?例如,如果您在圆括号的末尾加上一个),该怎么办?

What happens if you ever run into a situation where s is empty, you have something else coming in from std::cin? For instance, what if you had an extra ) at the end of a parenthesis set like this:

()()) // <-- extra )

()被取消,但)仍然存在.因此,当您尝试引用顶部时,什么也没有.这可能是导致崩溃的原因.

The ()s cancel out, but that ) is still there. So, when you try to reference the top, nothing is there. This is probably what is causing the crash.

在尝试引用顶部之前,需要对此行进行检查以确保 s 不为空:

You need a check around this line to make sure s isn't empty before you try to reference the top:

if(!s.empty()) {
    comp=s.top();
}

您还应该在 pop()周围执行此操作:

You should also probably do this around pop() as well:

if(c==')' && comp=='(' && !s.empty())
           s.pop();

或者类似的东西.您的逻辑可能需要重新设计,但希望可以为您提供思路.

Or something to that effect. Your logic may need some reworking, but hopefully that gives you the idea.

这篇关于休息;导致段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆