程序在直接运行时在realloc崩溃,但在调试模式下运行良好
我试图在插入或删除新节点时通过动态分配和释放内存来实现二进制堆。因此,每当调用insert/delete节点时,我都使用realloc来增加/减少内存。 程序在调试模式下运行良好,但当我直接运行它时,它会崩溃(可能是在realloc) 我的理由是,如果我删除delete函数中的realloc(这意味着我永远不会释放已经分配的内存),那么程序在直接运行时运行良好。 代码中可能存在什么问题 附言:我正在Windows10上使用EclipseCDT和Cygwin程序在直接运行时在realloc崩溃,但在调试模式下运行良好,c,heap,realloc,C,Heap,Realloc,我试图在插入或删除新节点时通过动态分配和释放内存来实现二进制堆。因此,每当调用insert/delete节点时,我都使用realloc来增加/减少内存。 程序在调试模式下运行良好,但当我直接运行它时,它会崩溃(可能是在realloc) 我的理由是,如果我删除delete函数中的realloc(这意味着我永远不会释放已经分配的内存),那么程序在直接运行时运行良好。 代码中可能存在什么问题 附言:我正在Windows10上使用EclipseCDT和Cygwin #include <stdio.
#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;
}
#包括
#包括
#包括
#包括
typedef结构堆
{
uint32_t尺寸;
int32_t*heaparray;
}堆;
void insert_max(堆**h1,int32_t值)
{
uint32_t孔;
堆*h=*h1;
如果(h->size==0)
{
h->size=2;
h->heaparray=(int32_t*)(malloc(sizeof(int32_t)*h->size));
h->heaparray[0]=0;
h->heaparray[1]=值;
返回;
}
孔=h->尺寸++;
h->heaparray[0]=值;
h->heaparray=(int32_t*)(realloc(h->heaparray,sizeof(int32_t)*h->size));
//筛选
对于(;值>h->heaparray[hole/2];hole/=2)
{
h->heaparray[hole]=h->heaparray[hole/2];
}
h->heaparray[孔]=值;
}
无效打印堆(堆*h)
{
uint32_t指数;
printf(“\nHeap:”);
对于(索引=1;索引size;索引++)
{
printf(“%3d\t”,h->heaparray[index]);
}
}
void siftDown\u max(堆**h1,uint32\u t索引)
{
uint32_t右索引、左索引、最大索引、温度;
堆*h=*h1;
leftIndex=(2*索引);
rightIndex=(2*索引)+1;
如果(rightIndex>=h->size)
{
如果(leftIndex>=h->size)
返回;
其他的
{
maxIndex=leftIndex;
}
}
其他的
{
如果(h->heaparray[rightIndex]>=h->heaparray[leftIndex])
{
maxIndex=rightIndex;
}
其他的
{
maxIndex=leftIndex;
}
}
如果(h->heaparray[index]heaparray[maxIndex])
{
temp=h->heaparray[索引];
h->heaparray[index]=h->heaparray[maxIndex];
h->heaparray[maxIndex]=温度;
siftDown_max(h1,最大索引);
}
}
无效筛选最小值(堆**h1,uint32索引)
{
uint32_t右索引、左索引、最小索引、温度;
堆*h=*h1;
leftIndex=2*index;
rightIndex=(2*索引)+1;
如果(rightIndex>=h->size)
{
如果(leftIndex>=h->size)
{
返回;
}
其他的
{
minIndex=左索引;
}
}
其他的
{
如果(h->heaparray[leftIndex]heaparray[rightIndex])
{
minIndex=左索引;
}
其他的
{
minIndex=右指数;
}
}
如果(h->heaparray[index]>h->heaparray[minIndex])
{
temp=h->heaparray[minIndex];
h->heaparray[minIndex]=h->heaparray[index];
h->heaparray[索引]=温度;
siftDown\u min(h1,最小指数);
}
}
void Delete(堆**h1,bool maxflag)
{
uint32_t孔=0;
堆*h=*h1;
如果(h->size==1)
{
h=零;
返回;
}
其他的
{
孔=--h->尺寸;
h->heaparray[1]=h->heaparray[hole];
h->heaparray=(int32_t*)(realloc(h->heaparray,sizeof(int32_t)*h->size));
if(maxflag)
{
siftDown_最大值(h1,1);
}
其他的
{
siftDown_min(h1,1);
}
}
}
void insert_min(堆**h1,int32_t值)
{
uint32孔指数=0;
heap*local_heap=*h1;
如果(本地堆->大小==0)
{
本地堆->大小=2;
本地堆->heaparray=(int32\u t*)malloc(sizeof(int32\u t)*本地堆->大小);
本地_堆->heaparray[0]=0;
本地_heap->heaparray[1]=值;
返回;
}
洞索引=本地堆->大小++;
本地_heap->heaparray[0]=值;
对于(;值heaparray[hole_index/2];hole_index/=2)
{
本地_堆->heaparray[洞索引]=本地_堆->heaparray[洞索引/2];
}
本地\u堆->heaparray[孔\u索引]=值;
}
内部主(空)
{
int-hy=0;
heap*newheap=(heap*)(malloc(sizeof(heap));
newheap->size=0;
插入_max(&newheap,5);
插入_max(&newheap,3);
插入_max(&newheap,8);
插入_max(&newheap,2);
插入_max(&newheap,10);
插入_max(&newheap,13);
插入_max(&newheap,7);
插入_max(&newheap,26);
插入_max(&newheap,11);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
删除(&newheap,true);
打印堆(newheap);
插入_max(&newheap,134);
打印堆(newheap);
heap*minheap=(heap*)(malloc(sizeof(heap));
minheap->size=0;
插入_min(&minheap,5);
打印堆(minheap);
插入_min(&minheap,3);
打印堆(minheap);
插入_min(&minheap,8);
打印堆(minheap);
插入_min(&minheap,2);
打印堆(minheap);
插入_min(&minheap,10);
打印堆(minheap);
插入_min(&minheap,13);
打印堆(minheap);
插入_min(&minheap,7);
打印堆(minheap);
插入_min(&minheap,26);
打印堆(minheap);
插入_min(&minheap,11);
打印堆(minheap);
删除(&minheap,false);
打印堆(minheap);
删除(&minheap,false);
打印堆(minheap);
删除(&minheap,false);
打印堆(minheap);
删除(&minheap,false);
打印堆(minheap);
删除(&minheap,false);
打印堆(minheap);
删除
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct heap
{
uint32_t size;
int32_t* heaparray;
// START DEBUG CODE
uint32_t debug_allocated_size; // contains the actual allocated size
// END DEBUG CODE
}heap;
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);
// START DEBUG CODE
local_heap->debug_allocated_size = local_heap->size;
// END DEBUG CODE
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)
{
// START DEBUG CODE
if (local_heap->debug_allocated_size >= hole_index)
{
// if hole_index is larger than the actuallly allocated size there is a problem...
printf("argh: buffer overflow\n");
exit(1);
}
// END DEBUG CODE
local_heap->heaparray[hole_index] = local_heap->heaparray[hole_index / 2];
}
local_heap->heaparray[hole_index] = value;
}
int main(void)
{
heap *minheap = (heap *)(malloc(sizeof(heap)));
minheap->size = 0;
insert_min(&minheap, 5);
insert_min(&minheap, 3);
insert_min(&minheap, 8);
insert_min(&minheap, 2);
return EXIT_SUCCESS;
}
h->heaparray = (int32_t *)(realloc(h->heaparray , sizeof(int32_t) * h->size));
typedef struct heap
{
uint32_t size; /* size of the heap array */
uint32_t count; /* number of items currently in the heap */
int32_t* heaparray;
}heap;