Ad
  • Default User Avatar

    It's because numbers.size() returns a size_t (unsigned int), so when the array is empty numbers.size() - 1 is a large unsigned number (0xFFFFFFFF). Then the for loop executes that number of times and an invalid array index is used.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    still does not work. using exactly what you did and still having a segmentation fault. makes no sense.

  • Custom User Avatar

    Well if you got a SIGSEGV (signal: segmentation violation), then it's likely that your program tried to access a spot in the vector numbers that was out of bounds. This might be because:

    a) you used ++i in the for loop control instead of i++

    or

    b) you didn't include the -1 in the for loop control (needed to stay in bounds when grabbing [i+1] inside the loop)

    Hope this was helpful!

  • Default User Avatar

    Hello! Recently, I tried to solve the problem with the similar method, but it told me "Process exited prematurely with a SIGSEGV signal. "
    So, if you know the reason, could you please tell me? Thanks.