简体   繁体   中英

Why is my program giving me an Abort Trap: 6 error?

This code is supposed to sort the array of strings, but around the second iteration of the main loop in selection sort, it gives me an Abortion Trap: 6 error. I am running it on the terminal in a Mac. Here is the code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int letSize = 20;
int vecSize;
char **array1;

void selectionSort (int low, int high)
{
    char *temp = malloc ((letSize + 1) * sizeof (char));
    int i = 0, j = 0;

    for (i = low; i < high - 1; i++) {
        int indexOfMin = i;
        for (j = i + 1; j < high; j++)
            if (strcmp (array1[j], array1[indexOfMin]) < 0)
                indexOfMin = j;
        //after second main loop, error occurs
        strcpy (temp, array1[i]);
        strcpy (array1[i], array1[indexOfMin]);
        strcpy (array1[indexOfMin], temp);
    }
}

int main ()
{
    int i, j;

    printf ("Enter size of items to be sorted: ");
    scanf ("%d", &vecSize);
    array1 = malloc (vecSize * sizeof (char *));
    for (i = 0; i < vecSize; i++)
        array1[i] = malloc ((letSize + 1) * sizeof (char));

    srand (time (NULL));
    for (i = 0; i < vecSize; i++) {
        for (j = 0; j <= letSize; j++) {
            if (j != letSize) {
                char randLet = 'A' + (random () % 26);
                array1[i][j] = randLet;
            } else
                array1[i][j] = '\0';
        }
    }
    selectionSort (0, vecSize);
}

This is the code that is giving me trouble. It compiles without any problems and it also takes the input from the user, but after wards it gives me the error of abort trap: 6. What could be causing this? Thanks in advance.

The problem is you attempt a copy when j == indexOfMin (or when j == i ) which attempts to copy overlapping memory regions with strcpy (you can with memmove , not strcpy ). From man strcpy

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\\0'), to the buffer pointed to by dest. The strings may not overlap , ....

You simply need a check for and copy only if j != indexOfMin to prevent trying to copy a string over itself. eg:

void selectionSort (int low, int high)
{
    char *temp = malloc ((letSize + 1) * sizeof (char));
    int i = 0, j = 0;

    for (i = low; i < high - 1; i++) {
        int indexOfMin = i;
        for (j = i + 1; j < high; j++)
            if (strcmp (array1[j], array1[indexOfMin]) < 0)
                if (j != indexOfMin) {
                    indexOfMin = j;
                    strcpy (temp, array1[i]);
                    strcpy (array1[i], array1[indexOfMin]);
                    strcpy (array1[indexOfMin], temp);
                }
    }
    free (temp);
}

Also remember to free (temp) or you have a guaranteed memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM