크기 조정 가능한 배열을 사용하여 힙을 구현 중이며 메모리를 재 할당 할 때마다이 오류가 계속 발생합니다. 문제는 realloc과 함께입니다. 나는 그저 잘못된 점을 알아낼 수 없습니다. data_t 선언있어 여기double free 또는 corruption (fasttop) 오류
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));
:
typedef struct{
int arraySize;
int* heapElemOne;
long* heapElemTwo;
int size;
int number;
pthread_mutex_t mutex;
pthread_cond_t more;
}data_t;
그것을 변화하고 때 4에 메모리를 이전하지만, 여기에 삽입 기능입니다 :
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;`
그리고 여기에 주요의 일부입니다 8로 설정하면 오류가 발생합니다. 나이가 들면서 그걸 알아낼 수 없습니다. -_- 미리 감사드립니다! malloc
및 realloc
에
http://stackoverflow.com/ help/mcve –
실제로 필요한 것이 무엇인지 파악할 수 없습니다. – Gopi
'malloc()'과'realloc()'을 호출 할 때마다 'sizeof (int)'또는'sizeof (long)'에 의해 할당 될 원소의 수를 당신이 저장하는 데이터? – ua2b