Thread: faliure to write a recursive function causes segfault

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well as a thought experiment, you need to make sure your code handles a base case properly.
    Code:
    int data[1] = {0}, num_elements = 1;
    moveelements(data, 0, num_elements - 1);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the reason i had if i <= end was i < end leaves the 0 at index 1 not start mind you as this is going to be for sorting arrays it seems silly to pass in a single number

  3. #18
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    as long as i is NEVER greater than end then your fine

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cooper1200 View Post
    the reason i had if i <= end was i < end leaves the 0 at index 1 not start mind you as this is going to be for sorting arrays it seems silly to pass in a single number
    Any recursive function needs to be tested for all stopping conditions. An array with a single value needs to be either a stopping condition or the function needs to test for it as a "starting" condition.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #20
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Here's a more readable way to do your function (with a couple of fail safes):
    Code:
    void moveelements(int data[], int pos, int end)
    {
    	int i, j;
    	if ( pos >= end ) return;
    	if ( pos < 0 ) pos = 0;
    	for ( i = pos, j = i + 1; i < end; ++i, ++j ) {
    			if ( data[j] == 0 ) break;
    	}
    	if ( i == end ) return;
    	data[j] = data[i];
    	data[i] = 0;
    	moveelements(data, pos, i - 1);
    }
    Last edited by awsdert; 05-29-2019 at 05:17 PM. Reason: Forgot a failsafe

  6. #21
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i have implemented your suggestions and changed the code slightly so i can move the 0 to any position in the array
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void moveelements(int data[], int pos, int start, int end);
    void printelements(const int x, const int data[]);
    
    int main()
    {
        int data[6] = {2, 3, 4, 5, 6, 0}, num_elements = 6;
        //int test[1] = {0};
    
        printelements(num_elements, data);
        moveelements(data, 3, 3, num_elements - 1);
        printelements(num_elements, data);
        return 0;
    }
    
    void moveelements(int data[], int pos, int start, int end)
    {
        int i, j;
    
        if (pos < start || pos >= end)
        {
            return;
        }
        if (pos < 0)
        {
            pos = 0;
        }
        if (pos <= end)
        {
            for (i = pos, j = i + 1; i < end; i++,  j++)
            {
                if (data[j] == 0)
                {
                    break;
                }
            }
            if (i == end)
            {
                return;
            }
            data[i + 1]= data[i];
            data[i] = 0;
            moveelements(data, i - 1, start, end);
        }
    }
    
    void printelements(const int x, const int data[])
    {
        int i;
    
        for (i = 0; i < x; i++)
        {
            printf("%d ", data[i]);
        }
        printf("\n");
    }
    however i am wondering if this is truly recursive as all the work is done before the recursive call not after im just trying to save some leg work
    coop

  7. #22
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    here is the final goal
    Code:
    //two arrays data[x] sorteddata[x]
    //have an index for each array
    //insert data[0] into sorteddata[0]
    //increment dataindex
    //look at data[1] and compare with sorted data[0]
    //if data[1] less than sorteddata[0] move sorteddata[0] to sorteddata[1]  insert data[1] at sorteddata[0]
    //increment sortedindex by 2 and dataindex by 1
    //else data[1] is greater or equal to sorteddata[0] so increment sortedindex by 1 and insert data[1] into sorteddata[1]
    //loop
    Last edited by cooper1200; 05-30-2019 at 03:17 AM.

  8. #23
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by cooper1200 View Post
    save some leg work
    Then why the recursive function? Simpler to do something like this:
    Code:
    // Dont' need a start positon, pos IS the start position
    int find_position_of_value( int data[], int pos, int end ) {
    int i, j;
    // Avoids unneeded operations
    if ( pos >= end ) return;
    // Avoids starting a segfaulting position
    if ( pos < 0 ) pos = 0;
    for ( i = pos, j = pos + 1; i < end; ++i, ++j ) {
      if ( data[j] == val ) return j;
    }
    return -1;
    }
    int move_backward( int data[], int dst, int pos ) {
    int i, j, val;
    // Avoids unneeded operations
    if ( pos == dst ) return 0;
    if ( dst < 0 ) return -1;
    if ( pos < dst ) return -1;
    val = data[pos];
    for ( i = pos, j = i - 1; i > dst; --i, --j ) {
      data[i] = data[j];
    }
    data[dst] = val;
    return 0;
    }
    int move_foreward( int data[], int pos, int dst ) {
    int i, j, val;
    // Avoids unneeded operations
    if ( pos == dst ) return 0;
    if ( pos < 0 ) return -1;
    if ( dst < pos ) return -1;
    val = data[pos];
    for ( i = pos, j = i + 1; i < dst; ++i, ++j ) {
      data[i] = data[j];
    }
    data[dst] = val;
    return 0;
    }
    int main( int argc, char *argv[] ) {
      int data[] = { 0, 2, 4, 6, 78 }, count = 5,
      pos = find_position_of_value( data, 78 ),
      two = find_position_of_value( data, 2 );
      if ( move_backward( data, two, pos ) != 0 ) {
        return 1;
      }
      return 0;
    }

  9. #24
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Made some slip ups in the code there, thought I edited them out but they still there, top function was supposed to have another value ("int val") and I forgot to add the first position and last position when calling it at the bottom

  10. #25
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    sorry i have been busy today and will be tomorrow please dont think i have just ignored your post

  11. #26
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    nw, I work a 7 hour shift, 6 days a week so fully understandable, tbh I actually forgot until just now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Attempting to write recursive function
    By pctechtv in forum C Programming
    Replies: 5
    Last Post: 08-29-2015, 12:25 PM
  2. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  3. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  4. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  5. Trying to write a recursive directory listing
    By crazeinc in forum C Programming
    Replies: 4
    Last Post: 04-28-2005, 12:35 AM

Tags for this Thread