views:

101

answers:

3

If I pass a value greater than 100 as the second argument to BinaryInsertionSort, I get a segmentation fault.

int
BinarySearch (int a[], int low, int high, int key)
{
    int mid;

    if (low == high)
        return low;

    mid = low + ((high - low) / 2);

    if (key > a[mid])
        return BinarySearch (a, mid + 1, high, key);
    else if (key < a[mid])
        return BinarySearch (a, low, mid, key);

    return mid;
}

void
BinaryInsertionSort (int a[], int n)
{
    int ins, i, j;
    int tmp;

    for (i = 1; i < n; i++) {
        ins = BinarySearch (a, 0, i, a[i]);
        if (ins < i) {
            tmp = a[i];
            memmove (a + ins + 1, a + ins, sizeof (int) * (i - ins));
            a[ins] = tmp;
        }
    }
}
+1  A: 

Could be because of stack overflow? You're calling BinarySearch recursively. The greater the value of n, the more stack space you'll consume. I would expect that you'd see a stack overflow error in such a case, but I don't know your environment...

Assuming that you aren't using a debugger, a quick way to test this would be to first find the exact point at which you get the error (you mentioned 100, but it's not clear that you wouldn't get the error with 99...).

Once you have that, try increasing the amount of stack space consumed by each recursive call to BinarySearch (add a few additional local variables, and do enough with them that they won't be optimized out). You should see that you can no longer successfully do 99 (or whatever your previous maximum was).

Scott Smith
Stack overflow seems unlikely if it happens on an array of size n=100. That's a recursive call depth of only 7 in a binary search, and the stack size of the recursive function is negligible.
David Gelhar
@David Gelhar : I thought about that, but since he doesn't show his `main` function, I had no idea how deep in a call stack he might already be, but yeah, point taken.
Scott Smith
+1  A: 

Hard to tell, Use a debugger. It will help you locate where the segmentation fault is located, and what are the value of different variable when the segmentation fault occurs.

  • on linux: gdb (to compile use g++ with the -g option)
  • on windows: the integrated debugger of visual studio C++
Phong
+4  A: 

You are passing an array a[] in. it must be large enough that the values hi and low are in range.

For example, if you pass an array of size 1 in, and low = 0. hi = 2, then mid = 1 which will be out of range (an array of size 1 can only have a[0] dereferenced, a[1] will be out of range).

Larry Watanabe
Yes, it's because of the size of the array. Originally the array size was 100. If I increase the array size, the error goes away, unless I specify a number greater than the new size. But why does the array size have to be as big as the value? If I wanted to have an array of 100 integers, why couldn't those integers be arbitrarily large?
Phenom
The integers can be arbitrarily large, up to the size of MAX_INT, but the space in which those integers are stored must be at addresses a up to a + hi - 1.
Larry Watanabe