[英]why am i getting for the following code AddressSanitizer: SEGV on unknown address error
'''
#include<stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> mystack;
char c;
for(int i=0;i<= s.length();i++){
if(s[i]=='(' || s[i]=='{' || s[i]== '[')
mystack.push(s[i]);
if(i!=0){
switch (s[i]){
case ')':
if(mystack.top()== '(')
mystack.pop();
else return false;
case '}':
if(mystack.top()== '{')
mystack.pop();
else return false;
case ']':
if (mystack.top()== '[')
mystack.pop();
else return false;
}
}
if(i==s.length() && !mystack.empty())
return false;
}
return true;
}
};'''
I dont know why but Iam getting this error AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000383dd4 bp 0x7ffc40c5d970 sp 0x7ffc40c5d860 T0) with hint ** address pointing to zero page** can someone please explain why iam getting this and how to resolve this.我不知道为什么但我收到此错误AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000383dd4 bp 0x7ffc40c5d970 sp 0x7ffc40c5d970 sp 0x7ffc40c5d860 T0)提示**地址指向零页**有人可以解释为什么我得到这个以及如何解决这个问题。 THANKS in advance!
提前致谢!
This code contained so many bugs.这段代码包含很多错误。 But the cause of the error actually was that inside the if condition of switch case
但错误的原因实际上是在switch case的if条件内
if( mystack.top() )
here I am accessing the top element of my stack without checking if it even exist or not hence the unknown address.在这里,我正在访问堆栈的顶部元素,而不检查它是否存在,因此是未知地址。 It should have been
应该是
if( !mystack.empty() && mystack.top())
This was compiler check if the address you are looking for exist or not if it does than only it will try to access the value.这是编译器检查您正在寻找的地址是否存在,如果它存在,它只会尝试访问该值。
I asked this as a noob programmer and after months of programming I debugged this myself.我作为一个菜鸟程序员问过这个问题,经过几个月的编程,我自己调试了这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.