问题
Okay so I need to create a 'generic' heapsort in c and this is what I have so far (I might be missing some closing brackets in code but they just got lost when I moved my code here)
void srtheap(void *, size_t, size_t, int (*)(const void *, const void *));
void heapify(void *, size_t, size_t, size_t, int (*)(const void *, const void *));
void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) {
void *p1, *p2;
void *last = base + (size*(nelem-1));
for (size_t curpos = nelem-1; curpos>0; curpos-2){
p1 = base + ((curpos-1)/2)*size;
if(compar(last, (last-size)) >= 0){
if(compar(last, p1) > 0){
swap(last, p1, size);
heapify(base, nelem, curpos, size, compar);
}
}
else { //LEFT>RIGHT
if(compar(last-size, p1) > 0){
swap(last-size, p1, size);
heapify(base, nelem, curpos-1, size, compar);
}
//otherwise, parent is greater than LEFT & RIGHT,
//or parent has swapped with child, iteration done, repeat through loop
} //end else, children have been compared to parent
//end check for two children, only left child if this loop is skipped
last = last-(2*size);
}
/*
**Now heapify and sort array
*/
while(nelem > 0){
last = base + (size*(nelem-1));
swap(base, last, size);
nelem=nelem-1;
heapify(base, nelem, 0, size, compar); //pass in array, #elements, starting pos, compare
}
}
void heapify(void *root, size_t numel, size_t pos, size_t sz, int (*compar)(const void *, const void *)){
void *rc, *lc, *p1;
while(pos < numel){
rc = root+((pos+1)*2)*sz; //right child
lc = root+(((pos+1)*2)-1)*sz; //left child
p1 = root+(pos*sz); //parent
if((pos+1)*2 < numel){ //check if current element has RIGHT
if (compar(rc, lc)>=0){
if(compar(rc, p1)>0) {
swap(rc, p1, sz);
pos=(pos+1)*2; //move to RIGHT, heapify
}
else {
pos = numel; //PARENT>LEFT&RIGHT, array is heapified for now
}
} //end RIGHT>LEFT
else { //LEFT>RIGHT
if(compar(lc, p1) >0 ) {
swap(lc, rc, sz);
pos=((pos+1)*2)-1; // move to LEFT, heapify
}
else {
pos = numel; //PARENT>LEFT&RIGHT, array is heapified for now
} //end inner if, else
}//end LEFT,RIGHT comparison
}//end check for RIGHT
else if (((pos+1)*2)-1 < numel){ //else, check if element has LEFT
if(compar(lc, p1)>0){
swap(lc, p1, sz);
pos=((pos+1)*2)-1; //move to LEFT, continue heapify
}
else {
pos = numel; //PARENT>LEFT, array is heapified for now
}
}//end check for LEFT
else { //current element has no children, array is heapified for now
pos = numel;
}
}
}
In addition I have a main file that includes a compare function. Essentially, the base address of the array, number of elements, size of each element, and the compare function are passed into my heapsort functions.
When I run the program I am getting a segmentation fault which I assume means I am trying to access memory that is not allocated to me. So I guess I'm wondering does anybody see any problems where I am accessing illegal memory addresses or could point me to a debugger that I can use to figure it out?
Thanks!
回答1:
Debuggers are often useful in diagnosing sources of memory errors. Let us know what happens when you run your code from a debugger.
回答2:
for (size_t curpos = nelem-1; curpos>0; curpos-2){
curpos-2
doesn't have any effect. Did you mean --
or -=2
?
Also, strictly speaking, you can't do pointer arithmetic on void *
pointers and even if your compiler allows it, don't rely on it. Instead, cast it to a char *
so that you are guaranteed that ptr + x
will only add x
bytes and not some multiple of x
.
You might also find it useful to create a macro to index into the element array. That would make your code a lot more readable:
#define ELEM(base, i) ((char *)(base) + (i)*size)
回答3:
You can use gdb to help debug this. First, compile your program with -g
to enable debugging symbols. Then run: gdb heapsort
where heapsort is the name of the program. Then type run
, hit enter, and wait until it crashes. Useful commands include bt
for backtrace and p
to print the values in variables.
来源:https://stackoverflow.com/questions/4259413/c-generic-heapsort