Why this pointer got a Segmentation fault C?

Lambda

Just as the follow code,the intcmp1 runs correctly but the intcmp gets a segment fault.I don't know why.These two codes looks as same.

My system environment is: OS X 10.10.2 64bit ; clang

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int intcmp(const void *v1, const void *v2){ //Segment Fault
    return (*((int*)(*(int*)v1)) - *((int*)(*(int*)v2)));
}
int intcmp1(const void *v1, const void *v2){ //No Problem
    return (**(int**)v1-**(int**)v2);
}
int main(int argc, char *argv[]) {
    int a[5]={0,1,2,3,4};
    int **b,i;
    b=calloc(5,sizeof(int*));
    for(i=0;i<5;i++){b[i]=&a[i];}

    printf("cmp1 begin\n");
    qsort(b,5,sizeof(int*),intcmp1);
    printf("cmp1 end\n");
    printf("cmp1 begin\n");
    qsort(b,5,sizeof(int*),intcmp);
    printf("cmp2 end\n");
}

Isn't **((int**)a) equal as *((int*)(*(int*)a))?

chqrlie

No, **((int**)a) and *((int*)(*(int*)a)) are not equivalent. The first one is correct in the context: a is indeed a pointer to an element of the array of int* passed to qsort. **((int **)a) or simply **(int**)a reads the integer you want to compare.

Conversely, the expression *((int*)(*(int*)a)) does something different: it reads from the same address in memory, but as an int and then pretends this int is actually an address and attempts to read from that address. If int and addresses don't have the same width, this will fail spectacularly. It they happen to be the same size, it will succeed non portably.

Furthermore, you cannot reliably compare int values by just subtracting one from the other. For example INT_MIN < 1 but INT_MIN - 1 invokes undefined behaviour and most likely computes to INT_MAX, a positive value.

intcmp1 should be rewritten this way:

int intcmp1(const void *v1, const void *v2) { // works better
    return (**(int**)v1 > **(int**)v2) - (**(int**)v1 < **(int**)v2);
}

The < and > comparison operators return 1 or 0, thus imtcmp1 will return -1, 0 or 1 precisely.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why I got a segmentation fault?

From Dev

C array pointer segmentation fault

From Dev

Pointer-to-a-pointer in C throws segmentation fault

From Dev

Pointer Pointer leads to Segmentation Fault in C

From Dev

Got segmentation fault: iogetline.c

From Dev

Need some help in my C program,I've got a segmentation fault but I don't know why

From Dev

segmentation fault when trying to deference pointer : C

From Dev

Odd C pointer segmentation fault errors

From Dev

segmentation fault using char pointer (C)

From Dev

segmentation fault when trying to deference pointer : C

From Dev

Segmentation fault error with pointer array in C

From Dev

Segmentation Fault, pointer issue?

From Dev

Segmentation fault on pointer

From Dev

Segmentation fault, pointer issue

From Dev

Why is this queue implementation in C giving segmentation fault?

From Dev

Why is this giving me a segmentation fault in c?

From Dev

Why is strlen causing a segmentation fault in C?

From Dev

Segmentation fault in C. Trying to swap two values using pointer

From Dev

Segmentation fault in C while using int pointer argument

From Dev

C segmentation fault when using pointer to structure in a linked list

From Dev

C Segmentation fault in a Circularly Linked List when setting next pointer

From Dev

C passing dynamic array through pointer (Segmentation Fault)

From Dev

Segmentation fault in C. Trying to swap two values using pointer

From Dev

Segmentation fault in C while declaring large pointer array

From Dev

C - Pointer to dynamic array in struct "Segmentation fault (core dumped)"

From Dev

C segmentation fault when using pointer to structure in a linked list

From Dev

C - Function pointer applied to matrix. Segmentation fault error

From Dev

segmentation fault in c about using malloc, and char array pointer

From Dev

C++ Segmentation Fault From Null Pointer Solution?

Related Related

  1. 1

    Why I got a segmentation fault?

  2. 2

    C array pointer segmentation fault

  3. 3

    Pointer-to-a-pointer in C throws segmentation fault

  4. 4

    Pointer Pointer leads to Segmentation Fault in C

  5. 5

    Got segmentation fault: iogetline.c

  6. 6

    Need some help in my C program,I've got a segmentation fault but I don't know why

  7. 7

    segmentation fault when trying to deference pointer : C

  8. 8

    Odd C pointer segmentation fault errors

  9. 9

    segmentation fault using char pointer (C)

  10. 10

    segmentation fault when trying to deference pointer : C

  11. 11

    Segmentation fault error with pointer array in C

  12. 12

    Segmentation Fault, pointer issue?

  13. 13

    Segmentation fault on pointer

  14. 14

    Segmentation fault, pointer issue

  15. 15

    Why is this queue implementation in C giving segmentation fault?

  16. 16

    Why is this giving me a segmentation fault in c?

  17. 17

    Why is strlen causing a segmentation fault in C?

  18. 18

    Segmentation fault in C. Trying to swap two values using pointer

  19. 19

    Segmentation fault in C while using int pointer argument

  20. 20

    C segmentation fault when using pointer to structure in a linked list

  21. 21

    C Segmentation fault in a Circularly Linked List when setting next pointer

  22. 22

    C passing dynamic array through pointer (Segmentation Fault)

  23. 23

    Segmentation fault in C. Trying to swap two values using pointer

  24. 24

    Segmentation fault in C while declaring large pointer array

  25. 25

    C - Pointer to dynamic array in struct "Segmentation fault (core dumped)"

  26. 26

    C segmentation fault when using pointer to structure in a linked list

  27. 27

    C - Function pointer applied to matrix. Segmentation fault error

  28. 28

    segmentation fault in c about using malloc, and char array pointer

  29. 29

    C++ Segmentation Fault From Null Pointer Solution?

HotTag

Archive