0

I have been trying to implement a quick sort of array of arrays of chars in C but it's giving me a segmentation fault error that I am not able to debug. This is the code:

int partition(char **a, int left, int right)
{
    int i, j;
    char pivot[16];
    strcpy(pivot, a[left]);
    i = left;
    j = right + 1;

    while (1)
    {
        do
            i++;
        while (i <= right && strcmp(a[i], pivot) < 0);
        do
            j--;
        while (strcmp(a[j], pivot) > 0);
        if (i >= j)
            break;
        char t[16];
        strcpy(t, a[i]);
        strcpy(a[i], a[j]);
        strcpy(a[j], t);
    }
    char t[16];
    strcpy(t, a[left]);
    strcpy(a[left], a[j]);
    strcpy(a[j], t);
    return j;
}

void quickSortChar(char **a, int left, int right)
{
    int j;
    if (left < right)
    {
        j = partition(a, left, right);
        quickSortChar(a, left, j - 1);
        quickSortChar(a, j + 1, right);
    }
}

int main()
{
    char **arr = (char **)calloc(10, sizeof(char *));
    arr[0] = (char *)malloc(16);
    arr[1] = (char *)malloc(16);
    arr[2] = (char *)malloc(16);
    arr[0] = "patata";
    arr[1] = "berenjena";
    arr[2] = "alioli";

    quickSortChar(arr, 0, 2);
}

Update 1

Using strcpy does not work either:

int partition(char **a, int left, int right)
{
    int i, j;
    char pivot[16];
    strcpy(pivot, a[left]);
    i = left;
    j = right + 1;

    while (1)
    {
        do
            i++;
        while (strcmp(a[i], pivot) < 0 && i <= right);
        do
            j--;
        while (strcmp(a[j], pivot) > 0);
        if (i >= j)
            break;
        char t[16];
        strcpy(t, a[i]);
        strcpy(a[i], a[j]);
        strcpy(a[j], t);
    }
    char t[16];
    strcpy(t, a[left]);
    strcpy(a[left], a[j]);
    strcpy(a[j], t);
    return j;
}

Update 2

I have solved the warning by moving up the declaration.

Update 3

Fix while (i <= right && strcmp(a[i], pivot) < 0);

7
  • As asked recently, use strcpy() to copy a string. The arr[0] = "patata"; etc overwrites the pointers that you allocated. Commented Dec 19, 2021 at 20:01
  • 1
    You also must provide a declaration to satisfy the forward reference to function partition() or move the function. Don't ignore compiler warnings. Commented Dec 19, 2021 at 20:05
  • I have tried using strcpy and it does not work. I have also addressed the warning but with the same result. Commented Dec 19, 2021 at 20:06
  • You are allocating memory and immediately leaking it as you assign a static string; don't need malloc: char *arr[] = { "patata", "berenjena", "alioli" };, and you certainly don't need strcpy. Commented Dec 19, 2021 at 20:32
  • This just an example. In my real program I have the structure described in the question. Commented Dec 19, 2021 at 20:35

1 Answer 1

3

Notice that you check i didn't passed the length of a only after strcmp(a[i], pivot) < 0 hence you reach i=3 and then get dumped.

Change to

while (i <= right && strcmp(a[i], pivot) < 0);

I'd also suggest using calloc instead of malloc in order to initialize the arr

Sign up to request clarification or add additional context in comments.

7 Comments

You are right but this does not solve the issue :(
@AntonioGamizDelgado I've just run the code on my computer with no errors
Using calloc? or malloc?
I'm compiling with gcc -p t test.c && ./t
@AntonioGamizDelgado both, compiled with gcc -Wall tmp.c -o tmp
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.