void siftdown(struct heap *H, int i) {
int pIndex, lcIndex;
int siftKey = H->nodes[i];
int spotFound = 0;
pIndex = i;
printf(" ");
// printf("first:%d ", H->size);
while (2 * pIndex + 1 <= H->size - 1 && spotFound == 0) {
if (2 * pIndex < H->size - 1 &&
(H->nodes[2 * pIndex + 1] < H->nodes[2 * pIndex + 2])) {
lcIndex = 2 * pIndex + 2;
// printf("second:%d ", H->size);
} else {
lcIndex = 2 * pIndex + 1;
// printf("third:%d ", H->size);
}
if (siftKey < H->nodes[lcIndex]) {
H->nodes[pIndex] = H->nodes[lcIndex];
pIndex = lcIndex;
// printf("fourth:%d ", H->size);
} else {
spotFound = 1;
// printf("fifth:%d ", H->size);
}
}
H->nodes[pIndex] = siftKey;
}
void makeHeap(int n, struct heap *H) {
H->size = n;
printf(" ");
// printf("sixth:%d ", H->size);
for (int i = floor(n / 2); i >= 0; i--) {
//printf("\n%d %d", i, H->nodes[i]);
siftdown(H, i);
}
}`
Im doing a array implementation of a heap and between the makeHeap to siftDown pass the heaps size becomes 0, unless these two empty print statements are present.
Im assuming that the heap->size call is reading from a register rather than fetching from memory but I don't really know how to fix it.
You have an unchecked access to
H->nodes[2 * pIndex + 2]in this part of your code:The condition
2 * pIndex < H->size - 1is not the right one. This was already checked by thewhilecondition, so when executed, this will always evaluate to true. You need to check whether the node has a right child, with this condition:2 * pIndex + 2 < H->size.Because this is not checked, you may get into a situation where
lcIndexis out of range, and eventually that makespIndexout of range, leading to a write to that location withH->nodes[pIndex] = siftKey;. This results in undefined behaviour. For instance, if that memory location happens to be used bysize, then this assignment alters the size.I would also harmonise all these boundary checks, using
< H->sizeand not<= H->size - 1and other variants.Note a problem, but in
makeHeapyou don't need toflooras the division is an integer division. You can also do with one iteration less, starting atn/2-1.So: