leetcode报错:
AddressSanitizer:DEADLYSIGNAL ================================================================= ==43==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x00000034ca17 bp 0x7ffe82f08070 sp 0x7ffe82f07f40 T0) ==43==The signal is caused by a READ memory access. ==43==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 0x7f952a7800b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) AddressSanitizer can not provide additional info. ==43==ABORTING
可见,访问了未知地址.
原因:
代码中对空栈访问了栈顶元素
else if(s[i] == '}')
{
if(st.top() == '{')
{
st.pop();
}else
{
return false;
}
}
解决办法:
加入判空就行
else if(s[i] == '}')
{
if(!st.empty() && st.top() == '{')
{
st.pop();
}else
{
return false;
}
}