Thread: faliure to write a recursive function causes segfault

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    faliure to write a recursive function causes segfault

    i thought i would have a go at writing a function that moves a "space" ( a 0) down an array to a given index recursively rather than having to cal the function x number of times.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void moveelements(int data[], 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;
    
        printelements(num_elements, data);
        moveelements(data, 0, num_elements - 1);
        printelements(num_elements, data);
        return 0;
    }
    
    void moveelements(int data[], int start, int end)
    {
        int i = start;
        if (i < end)
        {
            while (i + 1 != 0 && i < end)
            {
                i++;
            }
            data[i + 1]= data[i];
            moveelements(data, start, i -1);
        }
    }
    
    void printelements(const int x, const int data[])
    {
        int i;
    
        for (i = 0; i < x; i++)
        {
            printf("%d ", data[x]);
        }
    }
    it probably wont come as a surprise that i get a seg fault as soon as moveelements is called. so i am unable to debug it as i have no pointer to where the issue is(it fails on the openeing bracket of the function.

    any advice
    coop

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    segfaults at
    Code:
    data[i +1]
    , did you mean for that to be in the loop above?

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    my theory was to increment i until data[i+1] = 0 or i is equal to the end of the array then move the zero down the line or move the elements up the line.

    the purpose of this was to try and sort an array into another array initially filled with zeros and inserting the number in the appropriate slot ie if we had a 7 and wanted to insert it into the array 456890 move the 8 and 9 up an index which means i can insert the 7 between the 6 and 8.

    i hope that makes things a little clearer
    coop

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    But you're still trying to reference the point beyond that 0 which doesn't exist, also it's easier to just add another variable in your function, store that 0 (which you already gave the position for I might add) and then move everything along using data[i] = data[i - 1] while i is greater than the start position, then when that is done just shove the 0 back in using that variable you stored it in

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    if i was equall to end then yes i would be going over the end of the array but due to the condition of the while loop i never will be unless of course there arn't any 0's inwhich case im in trouble because im trying to fit more elements into the array than slots

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Wrong, you give 5 as the stop position which is the last possible position to access, 0 to 5 are accessible, 6 is the number of elements, i can ONLY be 0 to 5 when trying to access that array, anything else will segfault as you experienced

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    maybe this will help
    Code:
    /*
    [3][6][2][1][9][7][0]
                    ^
                    |
                    i points here as i+1 is = 0
    */
    i hope the formatting holds
    as i +1 = 0 the while loop breaks as i + 1 != 0 is false so i is still pointing at index 5 so swap data between data[i] and data[1+1]
    call function again this time it only looks at 5 elements as the 5th element is 0 and so on

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    dang i have written i +1 not data[i+1]

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    guess i was doing a man look
    sorry

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    another way to clearly understand is this
    Code:
    int elements [] = { 0,1,2,3,4,5 }, num_of_elements = 6;
    Each number here is the same as it's position inside the array, 6 is not in the array and therefore not accessable for this array.

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    yep like 3 6 2 1 9 7 0 = 7 elements as in the drawn example

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by cooper1200 View Post
    yep like 3 6 2 1 9 7 0 = 7 elements as in the drawn example
    That is true but I think he was confusing what i could be in the way that the lua developers thought starting the positions at 1 was a good idea

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok to use the coded array rather than the commented example i is at index 4 (the 6) while loop breaks as index 5 = 0 so i +1 = 0 swap the values of index 4 and index 5 call again etc etc

  14. #14
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    here is the changed code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void moveelements(int data[], 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;
    
        printelements(num_elements, data);
        moveelements(data, 0, num_elements - 1);
        printelements(num_elements, data);
        return 0;
    }
    
    void moveelements(int data[], int start, int end)
    {
        int i = start;
        if (i <= end)
        {
            while (data[i + 1] != 0 && i < end)
            {
                i++;
            }
            data[i + 1]= data[i];
            data[i] = 0;
            moveelements(data, start, i - 1);
        }
    }
    
    void printelements(const int x, const int data[])
    {
        int i;
    
        for (i = 0; i < x; i++)
        {
            printf("%d ", data[i]);
        }
        printf("\n");
    }
    coop

  15. #15
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Working, prob won't get to properly look for another hour and a half

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