Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

null

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.

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.

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.

like image 650
schwingms Avatar asked Feb 02 '16 01:02

schwingms


1 Answers

The chunk vector ceases to exist at the end of the for loop body. It's still referenced by some thread. That's called a dangling reference, and it's not good.

The error that you see may however be related to Result. Its definition isn't provided (at the time of writing this answer) so it's difficult to say. Remember that as the one who's asking what's wrong with the code, probably aren't qualified to decide what's important or not to show.

like image 166
Cheers and hth. - Alf Avatar answered Oct 20 '22 04:10

Cheers and hth. - Alf