C++ basic_string::_M_construct null 在构造字符串的子向量后无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35143293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:28:57  来源:igfitidea点击:

basic_string::_M_construct null not valid after constructing subvector of strings

c++stringnull

提问by schwingms

My code is supposed to read in a text file and have multiple threads look through different chunks of lines for the longest palindrome. The size of the chunk (how many lines) is determined by a variable number of threads passed in as an argument. The original text file is stored in an std::vector where each index of the vector corresponds to the original file.

我的代码应该在文本文件中读取,并让多个线程查看最长的回文行的不同块。块的大小(多少行)由作为参数传入的可变线程数确定。原始文本文件存储在 std::vector 中,其中矢量的每个索引对应于原始文件。

When I pass the subvector chunk to findPalindome(), I get a 'C++ basic_string::_M_construct null not valid' and I can't figure out why. None of my strings should be NULL.

当我将子向量块传递给 findPalindome() 时,我得到一个“C++ basic_string::_M_construct null not valid”,我不知道为什么。我的任何字符串都不应该是 NULL。

When I pass original vector lines I get no errors so I'm assuming it has to do with how I'm creating the subvector.

当我传递原始矢量线时,我没有得到任何错误,所以我假设它与我如何创建子矢量有关。

Here's my code:

这是我的代码:

Result longestPalindrome(std::string str)
{

    int maxLength = 1;  // The result (length of LPS)
    int start = 0;
    int len = str.size();
    int low, high;

    // One by one consider every character as center point of 
    // even and length palindromes
    for (int i = 1; i < len; ++i)
    {
        // Find the longest even length palindrome with center points
        // as i-1 and i.  
        low = i - 1;
        high = i;
        while (low >= 0 && high < len && str[low] == str[high])
        {
            if (high - low + 1 > maxLength)
            {
                start = low;
                maxLength = high - low + 1;
            }
            --low;
            ++high;
        }

        // Find the longest odd length palindrome with center 
        // point as i
        low = i - 1;
        high = i + 1;
        while (low >= 0 && high < len && str[low] == str[high])
        {
             if (high - low + 1 > maxLength)
             {
                start = low;
                maxLength = high - low + 1;
             }
             --low;
            ++high;
        }
    }
    Result result = {0, 0, 0};
    return result;
}

void findPalindome(std::vector<std::string> chunk, Result& result)
{
    Result localLargest = {0,0,0};
    for (unsigned int i = 0; i < chunk.size(); i++)
    {
        Result loopLargest = longestPalindrome(chunk[i]);
        if (localLargest < loopLargest)
        {
            localLargest = loopLargest;
        }
    }
    result = localLargest;
}

Result
FindPalindromeStatic(Lines const& lines, int numThreads)
{
    std::vector<Result> results(numThreads, {0,0,0});;
    int chunkSize = lines.size() / numThreads; //lines is the original vector with all the lines in the file
    std::vector<std::thread> threads;
    int counter = 0;
    for (int i = 0; i < numThreads; i++)
    {
        std::vector<std::string>::const_iterator begin = lines.begin() + counter;
        std::vector<std::string>::const_iterator end = lines.begin() + ((i + 1) * chunkSize);
        std::vector<std::string> chunk(begin, end);
        threads.emplace_back(&findPalindome, std::ref(chunk), std::ref(results[i]));
        counter = ((i+1)*chunkSize);
    }
    for (int i = 0; i < numThreads; i++)
    {
        threads[i].join();
    }
    Result x = {0,0,0};
    return x;
}

Any help would be appreciated and this is my first stack question so sorry for any mistakes.

任何帮助将不胜感激,这是我的第一个堆栈问题,对于任何错误,我们深表歉意。

采纳答案by Cheers and hth. - Alf

The chunkvector ceases to exist at the end of the forloop body. It's still referenced by some thread. That's called a dangling reference, and it's very ungood.

chunk矢量停止在的结尾存在for循环体。它仍然被一些线程引用。这就是所谓的悬空引用,这是非常不好的。

The error that you see may however be related to Result. You don't provide its definition (or, you had not provided it at the time of writing this answer) so it's difficult to say. Remember that you as the one who's asking what's wrong with the code, is provably not qualified to decide what's important or not to show: if you knew, then you'd probably know what's wrong.

但是,您看到的错误可能与Result. 您没有提供其定义(或者,在撰写此答案时您还没有提供),因此很难说。请记住,作为询问代码有什么问题的人,您显然没有资格决定什么重要或不显示:如果您知道,那么您可能会知道什么是错误的。