簡體   English   中英

AddressSanitizer:比較時出現 DEADLYSIGNAL 錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM