tags:

views:

109

answers:

4
+2  Q: 

quick sort problem

I use qsort from C libary and I have datatype

Element_type **pElement and Element_type is struct typedef element_type {int ,char ....} 

example, and i call quicksor function with

qsort(*pElement,iCountElement,(size_t)sizeof(Element_type),compare);

and callback function

static int compare(const void *p1, const void *p2) {
    Element_type  *a1 = (Element_type  *)p1;
    Element_type *a2 =  (Element_type   *)p2;
    return ( (a2)->iServiceId < (a1)->iServiceId );
}

but I always get segmentation fault. Why?

+3  A: 

Your compare function should return whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.

Also if you want to sort for example an array of Element_Type then you would cast the void* to type Element_Type*. If your elements that you are trying to sort are Element_Type* then you would cast the void* to Element_Type**.

If the items you are trying to sort are of type Element_Type* the make sure you are allocating memory for each of those items and then initialized for each item before calling qsort.

Brian R. Bondy
think i know that but synthax why segmentation fault my pointer is: pElement = (Element_type **)malloc(sizeof(Element_type* )* iNewSize) ;
farka
@farka: You say you know about the return value but you aren't doing that.
Brian R. Bondy
i have used pElement = (Element_type **)malloc(sizeof(Element_type* )* iiNewSize) ;dann : pElement[iCountElement] = (Element_type *)(DMALLOC(sizeof(Element_type))); memset(pElement[iCountElement],"data",sizeof(Element_type)); after that i call qsort, but always problem!!11
farka
now programm is running but the list ist unsorted!!!!! :-((((
farka
@farka: I think the problem is probably in your return value, return 0 when they are the same. return 1 when elem 1 is greater, and -1 when elem 1 is smaller.
Brian R. Bondy
+1  A: 
   pElement = (Element_type *)malloc(sizeof(Element_type )* iiNewSize);

You should call qsort(pElement, ...), not qsort(*pElement, ...). The pElement declaration at the top of your post cannot be accurate.

Hans Passant
A: 
static int compare(const void *p1, const void *p2) {
    Element_type  *a1 = *(Element_type  *)p1;
    Element_type *a2 =  *(Element_type   *)p2;

    if( (a1)->iServiceId < (a2)->iServiceId )
    {
        return -1; // this means that a1 < a2 in your criteria
    }

    if( (a1)->iServiceId == (a2)->iServiceId )
    {
        return 0; // this means that a1 == a2 in your criteria
    }

    return 1; // this means that a1 > a2 in your criteria
}

Call qsort like this:

qsort(pElement,iCountElement,(size_t)sizeof(Element_type),compare);

LATER EDIT: give us a piece of code so we can see more problems if that's the case

Iulian Şerbănoiu
A: 

This is the easiest way to correct your compare function:

 static int compare(const void *p1, const void *p2) {
    Element_type  *a1 = (Element_type  *)p1;
    Element_type *a2 =  (Element_type   *)p2;
-   return ((a2)->iServiceId < (a1)->iServiceId );
+   return (a1->iServiceId) - (a2->iServiceId);
 }

I can't read the first code segment, so I won't make suggestions on your segfault.

nategoose