bin3 / balgo

A library of BAsic ALGOrithms
Apache License 2.0
11 stars 1 forks source link

Segmentation faults #2

Open cheparukhin opened 10 years ago

cheparukhin commented 10 years ago

Segmentation fault occurs at line 361 in ac_da_trie.h when executing AhoCorasick::Build function.

bin3 commented 10 years ago

Please show me your code.

cheparukhin commented 10 years ago

Segmentation faults and exceptions occur when Char type is not char (e.g. int). When I change it to char, everything seems to work normally.

Also you forgot to include somewhere - when including only "aho_corasick.h" an error occurs during the compilation ("use of incomplete type std::stringstream").

Here's the simplified example that I made to demonstrate the errors:

#include <cstdlib>
#include <vector>
#include <limits>
#include <sstream>
#include <balgo/mpm/aho_corasick.h>

using Char = int;

std::vector<Char> MakeRandomSequence(int max_size) {
    const int size = std::rand() % max_size + 1;
    std::vector<Char> random_sequence;
    random_sequence.reserve(size);
    for (int i = 0; i < size; ++i) {
        random_sequence.push_back(rand() % std::numeric_limits<Char>::max());
    }
    return random_sequence;
}

int main() {
    std::vector<std::vector<Char>> dict;
    for (int i = 0; i < 100; ++i) {
        dict.push_back(MakeRandomSequence(50));
    }

    balgo::AhoCorasick<Char, size_t> ahocor;
    for (const std::vector<Char>& entry: dict) {
        ahocor.Insert(entry.data(), entry.size());
    }
    ahocor.Build();

    const std::vector<Char> query = MakeRandomSequence(1000);
    std::vector<size_t> matches;
    ahocor.Match(query.data(), query.size(), &matches);
}