
[英]Runtime Error AddressSanitizer:DEADLYSIGNAL in leetcode
[英]AddressSanitizer:DEADLYSIGNAL error on comparison
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
我试图解决检查括号是否有效的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's,而这不是您需要的情况。
这是将这些技巧实现为工作代码的一种方法:
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.