Thread: Segmentation fault in call to recursive function

  1. #1
    Registered User tarajano's Avatar
    Join Date
    Apr 2014
    Location
    Barcelona, Spain
    Posts
    3

    Segmentation fault in call to recursive function

    Hello everyone,

    I am learning C by my self and I would like to have some help with an exercise.

    Task:
    To create a recursive function to sort elements in an array of integers.
    The function must start the sorting from the first element, and the recursion calls must go on until the last element in the array is sorted. In each step of recursion, the function must work only with the subset of array elements that have not been sorted yet.

    Problem:
    I am getting a 'Segmentation fault: 11' in the recursive call to the function (please, see the code below).

    Environment:
    Mac, OS X = Mavericks

    Thanks a lot in advance,

    Manuel

    Code:
    ////////////////////////////////////////////////////////////////////////////////
    ////////// Recursively sorting an array of ints.
    ////////// Arguments: array,  array size, index from where to start sorting.
    void sort_incr_array_int_recursive(int a[], int size, int i){
      int tmp, idx_small, small = a[i];
    
    
      // Locating the smaller element in the array section.
      for(int p = i; p < size; p++)
        if(a[p] < small)
          idx_small = p;
    
    
      // Assigning the smaller element to the position 
      // currently evaluated.
      tmp = a[i];
      a[i] = a[idx_small];
      a[idx_small] = tmp;
    
    
      if(i < size) // The segmentation fault is caused by the instruction below
         sort_incr_array_int_recursive(a, size, i + 1);
    }
    ////////////////////////////////////////////////////////////////////////////////

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Consider what happens if i is equal to size - which is what will happen on the deepest recursive call. The first loop will ftop through, and idx_small will be uninitialised. The code from line 16 accesses a[i] (undefined behaviour, since array indexing is zero based), a[idx_small] (undefined behaviour since idx_small is uninitialised), and modifies a[idx_small] (undefined behaviour, again, since idx_small is uninitialised - and overwriting some random area of memory).

    In general terms, a segmentation fault means the operating system has detected your program accessing (reading or modifying) memory it shouldn't. It is one possible result of code that executes undefined behaviour ... and, as I've pointed out, three lines in your code will exhibit undefined behaviour.


    NOTE: I haven't even checked if your algorithm makes sense.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User tarajano's Avatar
    Join Date
    Apr 2014
    Location
    Barcelona, Spain
    Posts
    3
    Hi grumpy,

    Thanks for your comments, I got it solved.
    Is there any argument to the gcc compiler that could notify about those undefined behaviour situations?
    I am compiling my code using: $gcc prog.c -o prog.x

    Best,

    Manuel

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not in general, no. There are lots of reasons the standard specifies some behaviours to be undefined (or doesn't define them in some cases, with the effect they are undefined). One of the reasons is that it can be technically impossible for a compiler to detect some of them. Yours, at least within current compiler technology, is one of those cases.

    That said, assuming you're using a recent version of gcc, it (like most modern compilers) can be configured to give additional warnings, some of which are related to (other) undefined behaviours. The three gcc options -Wall (turns on a bunch of useful warnings that are not enabled by default) -Wextra (turns on a bunch of additional warnings that are not enabled by -Wall) and -pedantic (turns on warnings that, strictly speaking, are demanded by the C standards). All of these options are disabled by default. -pedantic depends on choice of standard, so look up the -std options in the gcc documentation.

    Bear in mind there are plenty of ways to trigger undefined behaviours that will get past the compiler, even with additional warnings enabled. Enabling compiler warnings does not eliminate the need for the developer to work to properly understand the nuances of his/her own code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User tarajano's Avatar
    Join Date
    Apr 2014
    Location
    Barcelona, Spain
    Posts
    3
    Well, thanks a lot. I will try your suggestions of -Wall -Wextra and -pedantic and try to learn also form their messages.
    You have been most helpful.
    Best,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-05-2012, 04:21 PM
  2. Segmentation fault on recursive function
    By xbfish in forum C Programming
    Replies: 12
    Last Post: 10-12-2011, 10:44 AM
  3. Recursive function and segmentation fault
    By davidjg in forum C Programming
    Replies: 4
    Last Post: 02-11-2011, 10:23 AM
  4. seg fault with any function call
    By LegoMan in forum C Programming
    Replies: 5
    Last Post: 04-15-2009, 05:30 PM
  5. Segmentation fault: vector to function
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2008, 04:36 AM