Why am I getting a segmentation fault?

Posted by Phenom on Stack Overflow See other posts from Stack Overflow or by Phenom
Published on 2010-03-25T00:57:22Z Indexed on 2010/03/25 1:03 UTC
Read the original article Hit count: 373

Filed under:
|

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;
        }
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about segmentation-fault