我想在堆的插入函数中使用 C 中的 recalloc。这是代码:
typedef struct MaxHeap {
int size;
int* heap;
} MaxHeap;
void max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)
max_heap->heap = realloc((max_heap->size+1), sizeof * max_heap->heap);
max_heap[max_heap->size] = N_INF;
max_heap->size += 1;
increase_key(max_heap, key_index, max_heap->size, key)
}
我收到这个警告:
warning: passing argument 1 of ‘realloc’ makes pointer from integer without a cast [-Wint-conversion]
我尝试了这个修复:
max_heap->heap = realloc((max_heap->heap), (max_heap->size+1) * sizeof(*(max_heap->heap)));
这是正确的吗?
您已将参数交换为
realloc ()
.
(max_heap->size+1)
评估为
int
,但 realloc()
的第一个参数需要一个 void *
指针。我相信 OP 打算重新分配 heap
成员而不是 size
成员。替换为:
(max_heap->heap);