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.