int *create_array (int n, int m, int intitial_value){
int *array;
int index, count;
array=(int *) malloc(n*sizeof(int)); //allocate memory based on user input
for (index=0;index<n;index++){
array[index]=(int *) malloc(m*sizeof(int));
}
for (index=0;index<n;index++){
for (count=0;count<m;count++){
array[n][m]=intitial_value;
}
}
return *array;
}
还好奇我是否正确地将内存释放到主?
ptr=create_array (n, m, intitial_value);
free(ptr);
任何帮助非常感谢!感谢
答案 0 :(得分:1)
int **create_array (int n, int m, int intitial_value){
int **array;
int index, count;
array = malloc(n*sizeof(int*)); //allocate memory based on user input
for (index = 0; index < n; index++){
array[index] = malloc(m*sizeof(int));
}
for (index = 0; index < n; index++){
for (count = 0; count < m; count++){
array[index][count] = intitial_value;
}
}
return array;
}
这样:
ptr=create_array (n, m, intitial_value);
free(ptr);
应该是
int i;
for(i = 0; i < n; i++) {
free(ptr[i];)
}
free(ptr);