0

Where is the segmentation fault occurring in this program?

Thanks!

/**
 * helpers.c
*
 * Computer Science 50
* Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>
#include <math.h>
#include <stdio.h>

#include "helpers.h"

/**
* Returns true if value is in array of n values, else false.
*/
//Recursive Search
bool binsearch(int key, int array[], int min, int max, int end)
{
  int mid1 = 0;
  int mid = 0;
  if(max < min)
{
    end = 0;
    return false;
}
else
{
    mid1 = max / 2;
    mid = round(mid1);
    if(array[mid] < key)
    {
        return binsearch(key, array, mid + 1, max, end);    
    }
    else if(array[mid] < key)
    {
        return binsearch(key, array, min, mid - 1, end);
    }
    else
    {
        end = end + 1;
        return true;
    }
}
} 
/**
* Returns true if value is in array of n values, else false.
*/

bool search(int value, int values[], int n)
{   
 int j = 0;
 int end = 0;
binsearch(value, values, j, n, end);  
return end;   
}

/**
     * Sorts array of n values.
 */
void sort(int values[], int n)
{   
 int j = 0;
 for(int i = 0;i < n;i++)
 {
    for(int k = 0;k < n;k++)
    {
        if(values[k] < values[k + 1])
        {
        values[k + 1] = j;
        values[k + 1] = values[k];
        values[k] = j; 
        }
    }
 } 
 return ;
}

1 Answer 1

3

One of the skills that are indirectly taught in this class, as well as being one of the most important skills you must learn as a programmer is how to debug a program. You will run into seg faults frequently as a programmer, so you really need to learn how to isolate them yourself!

There are a number of techniques you can use. You could step through the program using the gdb debugger program until you find the line of code that triggers the seg fault. You could also temporarily add some printf() statements in the code and see which ones execute before the seg fault. By moving them forward and backward, you will eventually put them on either side of the line of code that triggers the seg fault.

Once you locate the line that triggers the seg fault, you next need to figure out if the problem is actually that line. Frequently, trying to access memory beyond the end of an array (or a string) will cause one. Trying to use the contents of a pointer when the pointer is set to NULL will also trigger a seg fault.

Frequently, the trigger line is the problem code, but sometimes, maybe often, the real problem is caused earlier and only triggered later. In that case, you need to look carefully at all the vars and functions involved in the line of code that triggers the error and then go backwards and look at all the code related to those values and functions.

Debugging is an art and a science. You really need to take the time to work through this problem and to learn the skills that go with it. It may be painful and frustrating at first, but asking others to do it will only delay the pain. These psets are fairly easy, especially in the beginning. If you don't start picking up the debugging skills now, the later psets will be that much more difficult to get running correctly, and later projects that you may wish to take on for yourself or an employer will simply be torture.

THis may not be the answer you were looking for, but it's the best advice you can get. Good luck and keep at it until you find the problem!

2
  • +1 I couldn't agree more. It's not hard to learn how to debug a program, and man it's SO helpful (and much better than a bunch of printfs)
    – ChrisG
    Commented Aug 26, 2015 at 19:49
  • Here is a nice GDB tutorial if the course's isn't good enough: unknownroad.com/rtfm/gdbtut/gdbuse.html
    – ChrisG
    Commented Aug 26, 2015 at 19:51

You must log in to answer this question.

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