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!
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
thanks for replying. I get it now. :)