【问题标题】:basic_string::_M_construct null not valid after constructing subvector of stringsbasic_string::_M_construct null 在构造字符串的子向量后无效
【发布时间】:2016-02-02 01:01:19
【问题描述】:

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

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

当我传递原始向量线时,我没有收到任何错误,所以我假设它与我创建子向量的方式有关。

这是我的代码:

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;
}

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

【问题讨论】:

    标签: c++ string null


    【解决方案1】:

    chunk 向量在for 循环体的末尾不再存在。它仍然被一些线程引用。这被称为悬空引用,它并不好。

    您看到的错误可能与Result 有关。没有提供它的定义(在写这个答案的时候)所以很难说。请记住,作为询问代码有什么问题的人,可能没有资格决定什么重要或不显示。

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 2022-01-11
      • 2021-12-16
      • 2019-02-08
      • 2023-01-17
      • 2019-06-23
      • 1970-01-01
      • 2019-10-26
      相关资源
      最近更新 更多