简体   繁体   English

在抛出'std :: bad_alloc'的实例后调用终止what():std :: bad_alloc中止

[英]terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted

I am getting a bad_alloc exception in my program. 我的程序中出现了bad_alloc异常。

These are the constraints: 这些是约束:

  • 1 <= T <= 10 1 <= T <= 10
  • The length of each string is at most 100000 and contains only lower case characters. 每个字符串的长度最多为100000,仅包含小写字符。

With those constraints, I am unable to figure out why my program is getting bad_alloc . 有了这些限制,我无法弄清楚为什么我的程序变得bad_alloc

#include <string>
#include <algorithm>
#include <iostream>
#include <vector>

class SuffixArray {
    std::vector<std::string> suffixes;
    size_t N;
public:
    SuffixArray(std::string& s) {
        N = s.length();
        suffixes.resize(N);
        for (size_t i = 0; i < N; i++)
            suffixes[i] = s.substr(i);
        std::sort(suffixes.begin() , suffixes.end());
    }
    ~SuffixArray() {
    }
    size_t lcp(std::string& s, std::string& t) {
        size_t N = std::min(s.length(), t.length());
        for (size_t i = 0; i < N; i++)
            if (s[i] != t[i]) 
                return i;
        return N;
    }    
};

int main ( int argc, char **argv) {
    int T;
    std::cin >> T;
    std::vector<size_t> results;

    for ( int i = 0; i < T; i++) { 
        std::string str;
        std::cin >> str;
        SuffixArray sa(str);
        size_t sol = 0;

        for ( std::string::iterator it = str.begin(); it != str.end(); ++it) {
            std::string target = std::string(it, str.end());
            sol += sa.lcp(str, target);            
        }
        results.push_back(sol);
    }
    for ( std::vector<size_t>::iterator it = results.begin(); it != results.end(); ++it) 
        std::cout << *it << std::endl;
    results.clear();

    return 0;
}

Because what you do here: 因为你在这里做什么:

  for (size_t i = 0; i < N; i++)
        suffixes[i] = s.substr(i);

is: Create N sub strings of lengths 0, 1, 2, ..., N The total amount of memory these are going to consume is: 1 + 2 + 3 + ... + N bytes. 是:创建长度为0,1,2,...,N的N个子字符串这些消耗的内存总量为: 1 + 2 + 3 + ... + N个字节。 Having good old Gauss at your hand you will find that the sum of the first N numbers is: N * (N + 1) / 2 有了好老高斯,你会发现前N个数的总和是: N * (N + 1) / 2

Now if you set N = 100,000 this results in about 5GB of memory consumption - which is larger than the max. 现在,如果设置N = 100,000,则会导致大约5GB的内存消耗 - 大于最大值。 2GB address space your program usually has unless you run it on a 64bit system. 除非您在64位系统上运行它,否则程序通常具有2GB的地址空间。

Edit : I don't know what the problem is you are trying to solve however your implementation seems strange: 编辑 :我不知道你试图解决的问题是什么,但你的实现看起来很奇怪:

You never ever use the computed suffixes: The only function of SuffixArray you are using is lcp which makes no reference to the stored suffixes vector. 你从来没有使用计算后缀:唯一的功能SuffixArray您使用的是lcp ,这使得没有提及存储suffixes载体。 So what you need them for in the first place? 那么你首先需要它们呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 收到错误“在抛出&#39;std::bad_alloc&#39;what() 的实例后调用终止:std::bad_alloc” - getting error "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" 在抛出“ std :: bad_alloc”实例的what():std :: bad_alloc实例后终止调用 - terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" 在抛出&#39;std::bad _alloc&#39; what(): std::bad_alloc in c++ 实例后调用终止 - Terminate called after throwing an instance of 'std::bad _alloc' what(): std::bad_alloc in c++ 在抛出 'std::bad_alloc' 的实例后调用终止 what(): std::bad_alloc 在推回向量后 - terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc after pushing back into a vector Node.js错误“在抛出&#39;std :: bad_alloc&#39;的实例后调用终止what():std :: bad_alloc” - Node.js error “terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc” 在运行我的代码时显示在抛出 'std::bad_alloc' what() 实例后调用终止:std::bad_alloc - On running my code is showing terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc C++ 错误:在抛出 'std::bad_alloc' what(): std::bad_alloc 的实例后调用终止 - C++ error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 如何修复 C++ 错误:在抛出“std::bad_alloc”实例后调用终止。 什么():std::bad_alloc - How to fix C++ error: terminate called after throwing an instance of 'std::bad_alloc'. what(): std::bad_alloc 从向量中删除指针...错误:在抛出“std::bad_alloc”实例后调用终止 what(): std::bad_alloc - Deleting a pointer from a vector... Error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 为什么在抛出'std :: bad_alloc'的实例后终止调用? - why terminate called after throwing an instance of 'std::bad_alloc'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM