'Caught std::exception, what(): basic_string::copy: __pos (which is 18446744073709551615) > this->size() (which is 3)

I recently try to check a substring of another string. The test thought resulting throwing an exception I couldn't quite understand:

#include <string>
#include <iostream>

bool solution(std::string const &str, std::string const &ending) {
  // char string to put the substring from str
  char tmpTest[ending.length()-1];
  
  // copying sub-string the length of ending from str
  std::size_t length = str.copy(tmpTest, ending.length(), str.length()-ending.length());
  // add end of line
  tmpTest[length] = '\0';
    
  if (tmpTest == ending)
    return true;
  else
    return false;

}

with the exception:

Caught std::exception, what(): basic_string::copy: __pos (which is 18446744073709551615) > this->size() (which is 3)

any hints? Thanks..



Solution 1:[1]

It is not really clear what you want to do, but if I understood correctly, you are checking if string ends with "ending"

Check this website to find working solutions: https://www.techiedelight.com/check-if-a-string-ends-with-another-string-in-cpp/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ĺ eky