Блог пользователя warlock

Автор warlock, 10 лет назад, По-английски

Recently I saw a code which was accessing the index of a vector equal to its size. The Line read

for (int i = 0; i <= nums.size(); i++) {
	max_cost[i][i] = min_cost[i][i] = nums[i];
}

The code is for the Spoj Question Pocket Money. Whole Code link.

As per my knowledge this should give segmentation fault instead the solution gets accepted. Please can someone tell me what is happening here, why it doesn't give segmentation fault!

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
10 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

It doesn't give segmentation fault, it gives undefined behavior, which means that anything can happen. You can read more here: http://en.cppreference.com/book/undefined_behavior