简体   繁体   中英

Qsort with pointer to field segmentation fault

I want to sort my pointer of type (*double)[3] according to 0 column pnt[i][0] . So for example from

4 3 4
2 1 2
9 4 5

I want

2 1 2
4 3 4
9 4 5

I have a call to the quicksort function

qsort (pnt, numOfRows, sizeof(double), forQs);

with forQs as follows

int forQs (const void *x, const void *y)
{
    const double **k = (const double **)x; 
    const double **l = (const double **)y;
    return (*k)[0] - (*l)[0];
}

I end up with segmentation fault and valgrind says it's because of forQs. I was exploring before a lot how to create it as I don't know qsort much, but I can't see there any mistake. Can anyone help?

edit: I also used:

pnt = (double (*)[3]) malloc(100 * sizeof(*pnt));

I tried to make two samples, because you are not good enough unknown. Please refer to it.

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

int forQs (const void *x, const void *y)
{
    const double **k = (const double **)x; 
    const double **l = (const double **)y;
    return (*k)[0] - (*l)[0];
}

int main(){
    double **pnt = malloc(100*sizeof(double(*)[3]));
    int i, j;

    for(i=0;i<3;++i)
        pnt[i] = malloc(sizeof(double[3]));

    memcpy(pnt[0], (double [3]){4.0, 3.0, 4.0}, sizeof(double[3]));
    memcpy(pnt[1], (double [3]){2.0, 1.0, 2.0}, sizeof(double[3]));
    memcpy(pnt[2], (double [3]){9.0, 4.0, 5.0}, sizeof(double[3]));

    {//print before sort
        int i,j;
        for(i=0;i<3;++i){
            for(j=0;j<3;++j)
                printf("%g ", pnt[i][j]);
            printf("\n");
        }
        printf("\n");

    }
    int numOfRows = 3;
//  qsort(pnt, numOfRows, sizeof(double*), forQs);
    qsort(pnt, numOfRows, sizeof(*pnt), forQs);
    {//print after sort
        int i,j;
        for(i=0;i<3;++i){
            for(j=0;j<3;++j)
                printf("%g ", pnt[i][j]);
            printf("\n");
        }

    }
    return 0;
}

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

int forQs (const void *x, const void *y)
{
    const double (*k)[3] = *(const double (**)[3])x; 
    const double (*l)[3] = *(const double (**)[3])y;
    return (*k)[0] - (*l)[0];
}

int main(){
    double (**pnt)[3] = malloc(100*sizeof(*pnt));
    int i, j;

    for(i=0;i<3;++i)
        pnt[i] = malloc(sizeof(double[3]));

    memcpy(pnt[0], (double [3]){4.0, 3.0, 4.0}, sizeof(double[3]));
    memcpy(pnt[1], (double [3]){2.0, 1.0, 2.0}, sizeof(double[3]));
    memcpy(pnt[2], (double [3]){9.0, 4.0, 5.0}, sizeof(double[3]));

    {//print before sort
        int i,j;
        for(i=0;i<3;++i){
            for(j=0;j<3;++j)
                printf("%g ", (*(pnt[i]))[j]);
            printf("\n");
        }
        printf("\n");

    }
    int numOfRows = 3;
    //qsort(pnt, numOfRows, sizeof(double(*)[3]), forQs);
    qsort(pnt, numOfRows, sizeof(*pnt), forQs);

    {//print after sort
        int i,j;
        for(i=0;i<3;++i){
            for(j=0;j<3;++j)
                printf("%g ", (*(pnt[i]))[j]);
            printf("\n");
        }

    }
    return 0;
}

Protip: typedef the thing you want work with and stick to it throughout.

Want vectors of 3 doubles?

typedef double vec3[3];

Now when you rewrite your code in terms of vec3 , the problems become readily apparent.

qsort (pnt, numOfRows, sizeof(double), forQs); /* was */
qsort (pnt, numOfRows, sizeof(vec3), forQs); /* now */

const double **k = (const double **)x; /* was */
const vec3* k = (const vec3*)x; /* now */

This way you automatically get correct code without having to think about how and when your arrays become pointers (hint: they don't).

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