c/c++
本帖最后由 18135437048 于 2022-4-16 10:44 编辑#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int* p = NULL;
int len = 0; //数组长度
void AddNumber(int data){
int* pNew = (int*)malloc(sizeof(int)*len);
if(p){
memcpy(pNew,p,sizeof(int)*len);
free(p);//问题****
}
p = pNew;
p = data;
}
int sum(int len){
int sum=0;
int i=0;
for(;i<len;i++){
sum+=p;
}
return sum;
}
int main(){
int length=0;
int i=length;
int value=0;
int result=0;
printf("please input arr len:\n");
scanf("%d",&length);
//p=(int *)malloc(length*sizeof(int));
for(;i<length;i++){
printf("please input arr[%d]=:\n",i);
scanf("%d",&value);
AddNumber(value);
}
result=sum(length);
printf("result=%d\n",result);
free(p);
getchar();
getchar();
}
动态数组free memory有疑问,最后的free(p) 对吗?运行报debug error
参考文章:https://blog.csdn.net/qq_34181098/article/details/104213844
页:
[1]