Thread: Insertion sort function crashing if 8 or more values?

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    7

    Insertion sort function crashing if 8 or more values?

    Thanks to everyone for checking out this post. I posted a while ago and Jim helped me out, and now I'm back with a stickier problem!

    This program I'm working on accepts an array size from the user, prompts the user to store that many integers, sorts them from smallest to largest, and then searches for duplicates with a simple for loop. The ultimate goal of the assignment was to display duplicates in an array, and the rest of the functions are just how I decided to reach that goal.

    Anyway, my program crashes if I choose an array size larger than 7. It sorts and displays duplicates perfectly with 7 or fewer arguments.

    The exact moment it crashes is after I enter the final value it prompts me for, so it appears my inputsize() function and my inputarray() function are working, and the error may be in the arrsort() function. Any help is appreciated! Code is below:

    Code:
    #include <stdio.h>
    
    int funcinputsize(int);
    void funcinputarray(int [], int size);
    void funcarrsort(int [], int size);
    void funcdupe(int [], int size);
    
    int main()
    {
        int SIZE = 0;
        int array[SIZE];
    
        SIZE = funcinputsize(SIZE);
        funcinputarray(array, SIZE);
        funcarrsort(array, SIZE);
        funcdupe(array, SIZE);
    
        return 0;
    }
    
    
    int funcinputsize(int SIZE)
    {
        printf("Input the size of your array in whole numbers\n");
        scanf("%d", &SIZE);
        printf("Your array will be %d integers in length\n\n", SIZE);
    
        return SIZE;
    }
    
    void funcinputarray(int array[], int SIZE)
    {
        int i;
    
        for (i = 0; i < SIZE; i++)
        {
            printf("Enter an integer for array value %d\n", i + 1);
            scanf("%d", &array[i]);
            printf("The value at array index %d is %d\n\n", i, array[i]);
        }
    
        return;
    }
    
    void funcarrsort(int array[], int SIZE)
    {
        int i, j, temp;
        
        for(i = 1; i <= SIZE - 1; i++)
        {
            j = i;
            
            while (j > 0 && array[j] < array[j - 1])
            {
                temp = array[j];
                array[j] = array[j - 1];
                array[j - 1] = temp;
                j--;
            }
        }
    
        printf("Your array, ordered in ascending integer order:\n\n");
        
        for (i = 0; i <= SIZE - 1; i++)
        {
            printf("%d  ", array[i]);
        }
        
        printf("\n\n");
        
        return;
    }
    
    void funcdupe(int array[], int SIZE)
    {
        int i;
        
        for (i = 0; i < SIZE; i++)
        {
            if (array[i] == array[i + 1])
            {
                printf("%d, located at index %d, is a duplicate of the value at index %d\n\n", array[i], i, i + 1);
            }
        }
        return;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    int SIZE = 0;
    int array[SIZE];
    This is undefined behavior. You cannot have a zero-sized array. Even if you change the value of SIZE later, the array will not change to reflect that. The quick solution here is to create the array after you've gotten the size.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    7
    That fixed everything (tested with 100-value array). Thank you so much for your help. Just moved the array declaration below funcinputsize() and got rid of the "= 0" for the SIZE variable. Any idea why the old code still worked with arrays that contained less than 8 values? Seems too much of a coincidence that the program broke with 8 values, since it is such a significant number in coding.

    You forum veterans are awesome and I sincerely appreciate all of your work. I am learning as much as possible from you all =D

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Any idea why the old code still worked with arrays that contained less than 8 values?
    That'd be quite compiler/system dependent. Possibly your compiler inserts padding so that you're scribbling on unused memory for a few elements, before you start writing to critical areas.

    Add the following after your declarations:
    Code:
    int diff = (unsigned char *)&SIZE - (unsigned char *)array;
    printf("%d, %d\n", diff, diff / (int)(sizeof(int)));
    Technically this is undefined behavior, but on any sane system it will show you the difference, in bytes, between the SIZE and array variables, as well as the size in ints. On my system, I see:
    Code:
    24, 6
    That means that there are 24 bytes between the two variables, enough space for 6 ints. Even though the array is 0 bytes long (whatever that means, since it's not valid C), there is still space reserved that it spills into. Perhaps your system is similar.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    7
    That seems to be exactly what was happening. With the erroneous 0-length array, the code you gave me returned 20 5. With 10 values, 52 13, and with 100 values, 420 105. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using insertion sort to sort a database by age
    By codemonkey2013 in forum C Programming
    Replies: 1
    Last Post: 04-29-2014, 05:05 PM
  2. insertion sort vs shell sort
    By johnmerlino in forum C Programming
    Replies: 34
    Last Post: 04-28-2014, 06:41 PM
  3. Insertion sort!
    By shan2014 in forum C Programming
    Replies: 9
    Last Post: 04-09-2014, 09:54 AM
  4. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  5. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM