所以我正在实现一个使用可调整大小的数组的堆,并且每次重新分配内存时都会收到此错误。问题是与realloc ..我只是无法弄清楚它有什么问题。这里是插入功能:双免费或腐败(fasttop)错误
void* insert (data_t *data, int room, long wake) {
if(data->size+1 == data->arraySize){
data->arraySize *= 2;
long l = (long)data->arraySize;
int* tempOne = realloc(data->heapElemOne, data->arraySize*sizeof(int));
long* tempTwo = realloc(data->heapElemTwo, l*sizeof(long));
if (tempOne != NULL &&tempTwo !=NULL){ //realloc was
data->heapElemOne = tempOne;
data->heapElemTwo = tempTwo;
}
else{ //there was an error
printf("Error allocating memory!\n");
free(data->heapElemOne);
free(data->heapElemTwo);
return;
}
}
data->size++;
int now = data->size;
/*Adjust its position*/
if(data->size >0){
while(data->heapElemTwo[now/2] > wake && ((now/2)!=0))
{
data->heapElemTwo[now] = data->heapElemTwo[now/2];
data->heapElemOne[now] = data->heapElemOne[now/2];
now /= 2;
}
}
data->heapElemTwo[now] = wake;
data->heapElemOne[now] = room;`
,这里是主要的部分:
int main(int argc, char *argv[]){
pthread_t r, c;
data_t data;
data.arraySize = 2;
data.size = 0;
long l = (long)data.arraySize;
data.heapElemOne = malloc(data.arraySize * sizeof(int));
data.heapElemTwo = malloc(l * sizeof(long));
这里的data_t声明:
typedef struct{
int arraySize;
int* heapElemOne;
long* heapElemTwo;
int size;
int number;
pthread_mutex_t mutex;
pthread_cond_t more;
}data_t;
它搬迁内存4,但是当它正在改变它到8它给出了一个错误。在它已经很长时间了,只是无法想象它-_- 在此先感谢!
http://stackoverflow.com/帮助/ mcve – 2014-12-04 06:09:02
无法弄清楚你实际需要什么。 – Gopi 2014-12-04 06:09:31
是否可以在每次调用'malloc()'和'realloc()'的时候,根据类型的大小,乘以'sizeof(int)'或'sizeof(long)'分配的元素数量你存储的数据? – ua2b 2014-12-04 06:11:51