“detected_simple_anagram”中的致命错误:地址0x00000000处的内存访问冲突:故障地址处没有映射

时间:2014-10-06 02:04:19

标签: c++ c++11 boost

当我编译以下用于检测字谜的代码时,我收到了名义错误。

anagram_test.cpp

#include "anagram.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

using namespace std;

BOOST_AUTO_TEST_CASE(no_matches)
{
    auto subject = anagram::ana("diaper");
    auto matches = subject.matches({"hello", "world", "zombies", "pants"});
    vector<string> expected;

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}


BOOST_AUTO_TEST_CASE(detects_simple_anagram)
{
    auto subject = anagram::ana("ant");
    auto matches = subject.matches({"tan", "stand", "at"});
    vector<string> expected{"tan"};

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(does_not_detect_false_positives)
{
    auto subject = anagram::ana("galea");
    auto matches = subject.matches({"eagle"});
    vector<string> expected;

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(detects_multiple_anagrams)
{
    auto subject = anagram::ana("master");
    auto matches = subject.matches({"stream", "pigeon", "maters"});
    vector<string> expected{"stream", "maters"};

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(does_not_detect_anagram_subsets)
{
    auto subject = anagram::ana("good");
    auto matches = subject.matches({"dog", "goody"});
    vector<string> expected;

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(detects_anagram)
{
    auto subject = anagram::ana("listen");
    auto matches = subject.matches({"enlists", "google", "inlets", "banana"});
    vector<string> expected{"inlets"};

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(detects_multiple_anagrams2)
{
    auto subject = anagram::ana("allergy");
    auto matches = subject.matches({"gallery", "ballerina", "regally", "clergy", "largely", "leading"});
    vector<string> expected{"gallery", "regally", "largely"};

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(detects_anagrams_case_insensitively)
{
    auto subject = anagram::ana("Orchestra");
    auto matches = subject.matches({"cashregister", "Carthorse", "radishes"});
    vector<string> expected{"Carthorse"};

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(does_not_detect_a_word_as_its_own_anagram)
{
    auto subject = anagram::ana("banana");
    auto matches = subject.matches({"Banana"});
    vector<string> expected;

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}

BOOST_AUTO_TEST_CASE(matches_accepts_string_arguments)
{
    auto subject = anagram::ana("ant");
    auto matches = subject.matches({"stand", "tan", "at"});
    vector<string> expected{"tan"};

    BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
#endif

anagram.h

#include <string>
#include <vector>
#include <map>
using namespace std;

class anagram
{
    public:
        string word;
        vector<string> matches(vector<string> possibleMatches )
        {
            vector<string> returnValue;
            map<char, int> letterCount;
            for(char& c : word)
            {
                letterCount[c]+=1;
            }

            map<char, int> currWordLetterCount;
            for(int i = 0; i < possibleMatches.size(); i++ )
            {
                currWordLetterCount.clear();
                for( char& c: possibleMatches[i])
                {
                    currWordLetterCount[c]+=1;
                }
                if(currWordLetterCount == letterCount)
                {
                    returnValue[i] = possibleMatches[i];
                }
            }

            return returnValue;

        }
        static anagram ana(string inputWord);

};


anagram anagram::ana(string inputWord)
{
    anagram Anagram;
    Anagram.word = inputWord;
    return Anagram;
}

这是使用Boost v1.55。我不知道从哪里开始寻找这个bug。我的GCC版本是4.7.3。我很确定这个错误在detects_simple_anagram

2 个答案:

答案 0 :(得分:3)

我只是快速浏览一下你的代码,但这是一个错误:

vector<string> returnValue;

// ...

returnValue[i] = possibleMatches[i];

您创建一个空向量,然后将其索引到它,导致未定义的行为。我想你想要:

returnValue.push_back( possibleMatches[i] );

答案 1 :(得分:1)

试试这个:

map<char, int> currWordLetterCount;
returnValue.resize(possibleMatches.size());  // resize to fit new size of data
for(int i = 0; i < possibleMatches.size(); i++ )