0

I'm getting a segmentation fault error and I can't figure out why.

void sort(int values[], int n)
{
    // TODO: implement a sorting algorithm
    for (int i = 0; i<n; i++)
    {
        if ((n!=i+1) && (n>1))
        {
            if (values[i] > values[i+1])
            {
                int max = values[i];
                values[i] = values[i+1];
                values[i+1] = values[max];
            }
        }
        else if ((n==i+1) && (n>1))
        {
            i=0;
            n-=1;
        }

    }
    return;
}

bool BinSearch(int key, int array[], int min, int max)
{
    // TODO: implement a searching algorithm
    int mid = (min+max)/2;
    if (key==array[mid] || key==array[min] || key==array[max]) 
    {
        return true;
    }
    else if (key>array[mid]) 
    {
        if (mid==max)
        {
            return false;
        }
        return BinSearch(key, array, mid+1, max);
    }
    else if (key<array[mid]) 
    {
        if (mid==min)
        {
            return false;
        }
        return BinSearch(key, array, min, mid-1);
    }
    return false;
}
7
  • Does the seg fault come from search or sort? May 24 '17 at 16:08
  • I'm not sure. I tried putting a print statement before the binsearch function is called and nothing printed, so it may be in the sort. May 24 '17 at 17:28
  • Anyone have anything to add here? I have yet to resolve this issue. Jun 1 '17 at 14:43
  • FYI I'm the only one who sees the comment and I have nothing to add. You might consider deleting and reposting the question to get more attention. Or adding a bounty. Also FYI Jun 1 '17 at 15:32
  • First of all, this code just will not compile as there is a syntax mistake in sort.
    – obesman
    Jun 11 '17 at 18:41
1

You have two problems here:

First is that your sort function does not sort. Just look here, for example:

if ((n<i+1) && (n>1))

This condition will NEVER be true as you loop for i < n. There are also other deficiencies. To correct all I just need to put the whole correct code for sort here, that is against the rules.

I would advise you to redo it completely and, if not working, post it here again.

The second problem, where segmentation fault comes from, is in Binsearch. The reason is that when, sooner or later, you get min==max==mid and still no match, you continue call Binsearch again, not returning false.

More over, when min=max=mid and, for example, key > array[mid], you still send to Binsearch new min = mid+1, so you go beyond the size of your array that means segmentation fault.

You can do something like that, for example:

 if (mid == max)
    {
        return false;
    }
    return BinSearch(key, array, mid+1, max);

and your segmentation fault should disappear (provided that you repair your sort function)

1
  • Thanks for the help. So I changed ((n<i+1) && (n>1)) to ((n!=i+1) && (n>1)). I also added the mid==max condition. I am still getting the segmentation fault error. I can't seem to figure out if anything is wrong with the sort code. It seems logical to me. I've updated my post to reflect these changes. Jun 20 '17 at 18:25
1

Much better now. Your Binsearch works fine, but sort doesn't.

Segmentation fault comes from here

values[i+1] = values[max]

As max is a value in haystack, not an index of haystack[]; Correct it and segmentation fault should disappear.

But there is another mistake that would prevent your sort from working as it should.

for() loop is designed to "blindly" accept i whatever it is, then i++, then check condition and go ahead. So if you set

i = 0;

somewhere in the loop's body, it will start next time with i++, that is i = 1, not with i = 0 as you plan. You need

i = -1;

to let it restart from i = 0.

1
  • Wow it's always the little things that I miss. Thanks! Jun 22 '17 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .