Я пытаюсь реализовать двоичную кучу с динамически выделяемой и свободной памятью, когда новые узлы вставляются или удаляются.Поэтому, когда вызывается узел вставки / удаления, я использую realloc для увеличения / уменьшения памяти.Программа работает нормально в режиме отладки, но когда я запускаю ее напрямую, она падает (возможно, в режиме realloc)
Мои соображения связаны с тем фактом, что если я удалю realloc внутри функции удаления (это означает, что я никогда не освобожусьуже выделенная память), программа работает нормально при прямом запуске.В чем может быть проблема в коде?
PS: я использую Eclipse CDT вместе с Cygwin в Windows 10
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct heap
{
uint32_t size;
int32_t* heaparray;
}heap;
void insert_max(heap** h1, int32_t value)
{
uint32_t hole;
heap* h = *h1;
if(h->size == 0)
{
h->size = 2;
h->heaparray = (int32_t *)(malloc(sizeof(int32_t) * h->size));
h->heaparray[0] = 0;
h->heaparray[1] = value;
return;
}
hole = h->size++;
h->heaparray[0] = value;
h->heaparray = (int32_t *)(realloc(h->heaparray , sizeof(int32_t) * h->size));
//sift up
for(; value > h->heaparray[hole/2]; hole /= 2)
{
h->heaparray[hole] = h->heaparray[hole/2];
}
h->heaparray[hole] = value;
}
void printheap(heap* h)
{
uint32_t index;
printf("\nHeap: ");
for(index = 1; index < h->size; index++)
{
printf("%3d\t", h->heaparray[index]);
}
}
void siftDown_max(heap** h1, uint32_t index)
{
uint32_t rightIndex, leftIndex, maxIndex, temp;
heap* h = *h1;
leftIndex = (2 * index);
rightIndex = (2 * index) + 1;
if(rightIndex >= h->size)
{
if(leftIndex >= h->size)
return;
else
{
maxIndex = leftIndex;
}
}
else
{
if(h->heaparray[rightIndex] >= h->heaparray[leftIndex])
{
maxIndex = rightIndex;
}
else
{
maxIndex = leftIndex;
}
}
if(h->heaparray[index] < h->heaparray[maxIndex])
{
temp = h->heaparray[index];
h->heaparray[index] = h->heaparray[maxIndex];
h->heaparray[maxIndex] = temp;
siftDown_max(h1, maxIndex);
}
}
void siftDown_min(heap** h1, uint32_t index)
{
uint32_t rightIndex, leftIndex, minIndex, temp;
heap* h = *h1;
leftIndex = 2 * index;
rightIndex = (2 * index) + 1;
if(rightIndex >= h->size)
{
if(leftIndex >= h->size)
{
return;
}
else
{
minIndex = leftIndex;
}
}
else
{
if(h->heaparray[leftIndex] <= h->heaparray[rightIndex])
{
minIndex = leftIndex;
}
else
{
minIndex = rightIndex;
}
}
if(h->heaparray[index] > h->heaparray[minIndex])
{
temp = h->heaparray[minIndex];
h->heaparray[minIndex] = h->heaparray[index];
h->heaparray[index] = temp;
siftDown_min(h1, minIndex);
}
}
void Delete(heap** h1, bool maxflag)
{
uint32_t hole = 0;
heap* h = *h1;
if(h->size == 1)
{
h = NULL;
return;
}
else
{
hole = --h->size;
h->heaparray[1] = h->heaparray[hole];
h->heaparray = (int32_t *)(realloc(h->heaparray , sizeof(int32_t) * h->size));
if(maxflag)
{
siftDown_max(h1, 1);
}
else
{
siftDown_min(h1, 1);
}
}
}
void insert_min(heap** h1, int32_t value)
{
uint32_t hole_index = 0;
heap* local_heap = *h1;
if (local_heap->size == 0)
{
local_heap->size = 2;
local_heap->heaparray = (int32_t*)malloc(sizeof(int32_t) * local_heap->size);
local_heap->heaparray[0] = 0;
local_heap->heaparray[1] = value;
return;
}
hole_index = local_heap->size++;
local_heap->heaparray[0] = value;
for(; value < local_heap->heaparray[hole_index/2]; hole_index /= 2)
{
local_heap->heaparray[hole_index] = local_heap->heaparray[hole_index / 2];
}
local_heap->heaparray[hole_index] = value;
}
int main(void)
{
int hy = 0;
heap *newheap = (heap *)(malloc(sizeof(heap)));
newheap->size = 0;
insert_max(&newheap, 5);
insert_max(&newheap, 3);
insert_max(&newheap, 8);
insert_max(&newheap, 2);
insert_max(&newheap, 10);
insert_max(&newheap, 13);
insert_max(&newheap, 7);
insert_max(&newheap, 26);
insert_max(&newheap, 11);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
Delete(&newheap, true);
printheap(newheap);
insert_max(&newheap, 134);
printheap(newheap);
heap *minheap = (heap *)(malloc(sizeof(heap)));
minheap->size = 0;
insert_min(&minheap, 5);
printheap(minheap);
insert_min(&minheap, 3);
printheap(minheap);
insert_min(&minheap, 8);
printheap(minheap);
insert_min(&minheap, 2);
printheap(minheap);
insert_min(&minheap, 10);
printheap(minheap);
insert_min(&minheap, 13);
printheap(minheap);
insert_min(&minheap, 7);
printheap(minheap);
insert_min(&minheap, 26);
printheap(minheap);
insert_min(&minheap, 11);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
Delete(&minheap, false);
printheap(minheap);
insert_min(&minheap, 138);
printheap(minheap);
hy = 3;
return EXIT_SUCCESS;
}