|
-
February 10th, 2017, 12:29 PM
#1
"Abort trap" error in c++
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:
#include <cstdio>
#include "insertionSort.h"
int main() {
// create array
int array[] = {9, 8, 10, 2, 4, 6, 50, 1};
// get size of the array
int size = sizeof(array) / sizeof(array[0]);
// display array before reversing
printf("Before sorting\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
// new line
printf("\n");
// sort only the second half of the array
insertionSort(array+(size/2), size);
// display the array again after reversing
printf("After sorting\n");
for (int i = 0;i < size; i++) {
printf("%d ", array[i]);
} // end of for loop
// new line
printf("\n");
} // end of main
insertionSort.cc
Code:
#include "insertionSort.h"
void insertionSort(int* array, int size) {
for (int i = 1; i < size; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j+1] = array[j];
j--;
} // end of while loop
// swap
array[j+1] = key;
} // end of for loop
} // end of insertionSort
-
February 10th, 2017, 12:44 PM
#2
Re: "Abort trap" error in c++
Code:
insertionSort(array+(size/2), size);
The second argument is the number of elements to sort. In this case the size of the passed array should be 4 not 8 as you are only sorting the second half of the array.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 10th, 2017, 01:26 PM
#3
Re: "Abort trap" error in c++
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|