AddressSanitizer: deadlyssignal比较错误



我试图解决检查括号是否有效的leetcode问题。在比较闭括号与堆栈的顶部元素时,我遇到了以下错误。此外,即使我尝试非常明显的比较,如1>0,也会出现错误。是什么导致了这个错误?

Error Description:
Runtime Error
AddressSanitizer:DEADLYSIGNAL
=
==33==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x000000344919 bp 0x7ffdbedbbbf0 sp 0x7ffdbedbbac0 T0)
==33==The signal is caused by a READ memory access.
==33==Hint: this fault was caused by a dereference of a high value address (see register values below).  Dissassemble the provided pc to learn which register was used.
#3 0x7f72702470b2  (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
AddressSanitizer can not provide additional info.
==33==ABORTING
class Solution {
public:
bool isValid(string s) {
char tmp;   
stack <char> c_stack;
char b10='(',b20='{',b30='[';
char b11=')',b21='}',b31=']';
for(int i=0;i<s.size();i++)
{
if(s[i]==b10||s[i]==b20||s[i]==b30)
c_stack.push(s[i]);
else
{
if(c_stack.empty())
continue;
tmp=c_stack.top();
if(tmp==b10 && s[i]==b11)
c_stack.pop();
else if(tmp==b20 && s[i]==b21)
c_stack.pop();
else if(tmp==b30 && s[i]==b31)
c_stack.pop();
else
break;             
}
cout<<c_stack.top();

}
return c_stack.empty();

}
};

将开始分隔符推入堆栈…当您应该有结束分隔符匹配那里....并可能提供更多的信息来查明错误。

同样,永远不要传递std::string的按值传递,除非绝对需要一个新的副本,这不是您需要的情况。

这里有一种方法可以将这些技巧实现到工作代码中:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stack>
#include <string>
#include <string_view>
bool isValid(const std::string& s) {
char tmp;
std::stack<std::pair<const char*, size_t>> c_stack;
// pair up delimiters, this wuill make matching easier to handle.
static const char* delim_pairs[] = {"()", "[]", "{}"};
for (size_t i = 0; i < s.length(); ++i) {
const auto c = s[i];
// check for opening delimiter
bool match = false;
for (auto& d : delim_pairs) {
if (c == d[0]) {
c_stack.push({d, i});
match = true;
break;
}
}
// no use checking for more if a match was found.
if (match) continue;
// check for closing delimiter
for (auto& d : delim_pairs) {
if (c == d[1]) {
if (!c_stack.empty()) {
// does it match?
if (!c_stack.empty() && c == c_stack.top().first[1]) {
c_stack.pop();
break;  // no use checking for more if a match was
// found.
}
// it doesn't match
std::cout << "Error: string: "" << s
<< "" unmatched opening delimiter '"
<< c_stack.top().first[0]
<< "'' at position: " << c_stack.top().second
<< "n";
return false;  // no use checking for more if error
} else {
std::cout << "Error: string: "" << s
<< "" unmatched closing delimiter '" << c
<< "'' at position: " << i << "n";
return false;  // no use checking for more if error
}
}
}
}
if (c_stack.empty()) return true;
auto& err = c_stack.top();
std::cout << "Error: string: "" << s << "" unmatched opening delimiter '"
<< err.first[0] << "'' at position: " << err.second << "n";
return false;
}
int main() {
assert(isValid("1+1"));
assert(isValid("(1+1)"));
assert(isValid("[(1+2) - (1)] + 2"));
assert(isValid("[(1+2) - {x}] [5,6] + 2"));
assert(!isValid("(1+2)-{x}] [5,6] + 2"));
assert(!isValid("(1+2)-x} [5,6] + 2"));
assert(!isValid("("));
assert(!isValid("([)"));
assert(!isValid("([{]})"));
return 0;
}

您可以在这里使用上面的代码:https://godbolt.org/z/v1x5x67j8

最新更新