We can take a simple example to demonstrate this fact. Suppose in a certain large array, we are trying to find the midpoint of the range [1000, INT_MAX]. Now, INT_MAX is the largest value the int data type can store. Even if 1 is added to this, the final value will become negative.
Also, start = 1000 and end = INT_MAX.
Using the formula: (start + end)/2,
the mid-point will be
(1000 + INT_MAX)/2 = -(INT_MAX+999)/2, which is negative and may give segmentation fault if we try to index using this value.
But, using the formula, (start + (end-start)/2), we get:
(1000 + (INT_MAX-1000)/2) = (1000 + INT_MAX/2 - 500) = (INT_MAX/2 + 500) which will not overflow.
Note : (end – start) may overflow if end < 0 or start < 0
Also, start = 1000 and end = INT_MAX.
Using the formula: (start + end)/2,
the mid-point will be
(1000 + INT_MAX)/2 = -(INT_MAX+999)/2, which is negative and may give segmentation fault if we try to index using this value.
But, using the formula, (start + (end-start)/2), we get:
(1000 + (INT_MAX-1000)/2) = (1000 + INT_MAX/2 - 500) = (INT_MAX/2 + 500) which will not overflow.
Note : (end – start) may overflow if end < 0 or start < 0
No comments:
Post a Comment