double * copyTemp=NULL;
double * output= malloc(sizeof(double*)*MallSize);
double test1[] = {1.2,2.2,3.1};
double test2[]={ 0.1,0.2,0.3,0.5,0.7,1.1,1.2,2.2};
int i=0;
int j=0;
addArrayInOrder(test1 ,test2,output , &counter,&i , &j,size1,size2);
ShrinkMemory ( MallSize, counter, output , copyTemp );
free(output); <- this part crashes for some reason
return 0;
我遇到的问题是,有时当我调用免费程序时,程序就会兑现。我在这段代码中没有看到任何错误,因为我已经将 temp 设置为 NULL,所以只有一件事指向数组是 ans 指针。所以我应该可以在程序结束时调用 free 来释放内存。
该功能看起来像
void addArrayInOrder(double *array1, double *array2, double *final, int *finalCounter, int *arry1Counter, int *array2Counter, int ArrSize1, int ArrSize2)
{
while (*arry1Counter < ArrSize1 && *array2Counter < ArrSize2)
{
if (isEqual(array1, array2, *arry1Counter, *array2Counter))
{
*(final + *finalCounter) = *(array1 + *arry1Counter);
arry1Counter++;
(*array2Counter)++;
(*finalCounter)++;;
}
else if (*(array1 + *arry1Counter) > *(array2 + *array2Counter))
{
*(final + *finalCounter) = *(array2 + *array2Counter);
(*array2Counter)++;
(*finalCounter)++;
}
else
{
*(final + *finalCounter) = *(array1 + *arry1Counter);
(*arry1Counter)++;
(*finalCounter)++;
}
}
}
int ShrinkMemory(int MaxSize, int MinSize, double *final, double *temp)
{
if (MaxSize != MinSize)
{
temp = realloc(final, MinSize * sizeof(double *));
if (temp == NULL)
{
printf("relloc failed ! exixting /n");
return 1;
}
else
{
printf("fsafda %p \n",temp);
printf("fsafda %p \n",final);
final=temp;
temp=NULL;
printf("fsafda %p \n",temp);
printf("fsafda %p \n",final);
}
}
return 0;
}
为什么会崩溃?我什至使用 prinf %p 打印出内存并且地址看起来正确