"Abort trap" error in c++

Discussion in 'Programmer's Corner' started by asilvester635, Feb 10, 2017.

  1. asilvester635

    Thread Starter Member

    Jan 26, 2017
    68
    3
    I have an array of size 8, and I send only the second half of the array to my insertion sort. It successfully sorts only the second half of the array, but there is a statement at the bottom, "Abort trap: 6". Below are my main.cc and my insertionSort.cc files. What is causing the "Abort trap: 6" error?


    Here is the output:
    Before sorting
    9 8 10 2 4 6 50 1
    After sorting
    9 8 10 2 1 4 6 50
    Abort trap: 6


    main.cc
    Code (Text):
    1.  
    2. #include <cstdio>
    3. #include "insertionSort.h"
    4.  
    5. int main() {
    6.  
    7.     // create array
    8.     int array[] = {9, 8, 10, 2, 4, 6, 50, 1};
    9.  
    10.     // get size of the array
    11.     int size = sizeof(array) / sizeof(array[0]);
    12.  
    13.     // display array before reversing
    14.     printf("Before sorting\n");
    15.     for (int i = 0; i < size; i++) {
    16.         printf("%d ", array[i]);
    17.     }
    18.  
    19.     // new line
    20.     printf("\n");
    21.  
    22.     // sort only the second half of the array
    23.     insertionSort(array+(size/2), size);
    24.  
    25.     // display the array again after reversing
    26.     printf("After sorting\n");
    27.     for (int i = 0;i < size; i++) {
    28.         printf("%d ", array[i]);
    29.  
    30.     } // end of for loop
    31.  
    32.     // new line
    33.     printf("\n");
    34.    
    35. } // end of main
    36.  

    insertionSort.cc
    Code (Text):
    1.  
    2. #include "insertionSort.h"
    3.  
    4. void insertionSort(int* array, int size) {
    5.  
    6.     for (int i = 1; i < size; i++) {
    7.         int key = array[i];
    8.         int j = i - 1;
    9.  
    10.         while (j >= 0 && array[j] > key) {
    11.             array[j+1] = array[j];
    12.             j--;
    13.         } // end of while loop
    14.  
    15.         // swap
    16.         array[j+1] = key;
    17.  
    18.         } // end of for loop
    19.  
    20. } // end of insertionSort
    21.  
     
  2. asilvester635

    Thread Starter Member

    Jan 26, 2017
    68
    3
    I figured it out. Since I only wanted to sort half of the array, the size that I send to my insertionSort should be divided by 2.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    19,730
    5,487
    Are you sure?

    The value size/2 is the length of the first half of the array.

    What is it the length of the second half?
     
  4. asilvester635

    Thread Starter Member

    Jan 26, 2017
    68
    3
    My program successfully sorts the second half of the array. My array size is 8. The first half contains 4, the other half (which is what I wanted to sort) is also 4, therefore I send only half of what the size. What is your point?
     
  5. Papabravo

    Expert

    Feb 24, 2006
    11,024
    2,120
    What happens if your array has an odd number of elements. Integer division will truncate any remainder. That is to say that 9 / 4 = 4
     
  6. asilvester635

    Thread Starter Member

    Jan 26, 2017
    68
    3
    I see. I've added an if else statement inside my insertionSort that checks to see if the size is odd or even.
     
  7. WBahn

    Moderator

    Mar 31, 2012
    19,730
    5,487
    Well, if the only array that you ever want to sort the second half of is an array of size 8, then I guess you are fine.

    But should you ever want to sort the second half of an array other than one that is of size 8, perhaps you might consider those other possibilities.

    Is the following statement true for every nonnegative value of the integer variable size?

    size == (size/2) + (size/2)

    Test it.

    Run the following code:

    Code (Text):
    1.  
    2. for (int size = 0; size < 10; size++)
    3.    printf("%i %s (%i/2) + (%i/2)\n", size, (size == (size/2) + (size/2))? "==" : "!=", size, size);
    4.  
    What do you notice?
     
  8. WBahn

    Moderator

    Mar 31, 2012
    19,730
    5,487
    And what do you do if it is odd?

    You can certainly do it with an if statement, but you can also calculate it directly.

    If I tell you the size of the array and the size of the first part of the array, can you use that information to tell me how many elements are in the rest of the array?

    Well, size/2 tells you how many elements are in the first "half" of the array. So how many elements are in the second "half" of the array?
     
  9. Papabravo

    Expert

    Feb 24, 2006
    11,024
    2,120
    How about:

    size - (size/2) ;​
     
  10. WBahn

    Moderator

    Mar 31, 2012
    19,730
    5,487
    I was rather hoping that he might spend some time figuring out the solution to this small problem instead of having it spoon fed to him.
     
  11. Papabravo

    Expert

    Feb 24, 2006
    11,024
    2,120
    I don't usually do that, but this one was kinda trivial. It's kinda like watching a favorite pet suffer -- I just couldn't bear it.
     
Loading...