求助”用free()总提醒Windows 已在 text.exe 中触发一个断点。其原因可能是堆被损坏“请看看代码帮我找一下错
#include <stdio.h>#include <stdlib.h>
int sum(int *num, int n);
int main(void)
{
int num = 0,i =0;
int *array = NULL;
printf("请输入一组数长度:");
scanf("%d",&num);
array = (int *)malloc(num,sizeof(int));
if (!array)
{
printf("内存分配不成功\n");
return -1;
}
printf("输入数据:");
for (i = 0;i < num;i++)
{
scanf("%d",&array[i]);
}
printf("所有元素之和sum = %d\n",sum(array,num));
free(array);//为什么这里怎么总是中断?这样释放不对吗?
return 0;
}
int sum(const int *num, int n)
{
int Sum = 0;
if (0 == n)
{
return 0;
}
else
{
n-=1;
Sum = sum(num,n);
Sum += num[n];
}
return Sum;
}