You need to sign in or sign up before continuing.×
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

    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!