c-分配和释放2D数组
我正在尝试编写两个函数,一个用于动态分配2D数组,另一个用于释放此2D数组:c-分配和释放2D数组,c,arrays,pointers,sigabrt,C,Arrays,Pointers,Sigabrt,我正在尝试编写两个函数,一个用于动态分配2D数组,另一个用于释放此2D数组: int allocate(int **array, unsigned int rows, unsigned int columns){ int i; for (i = 0; i < rows; i++) { array[i] = malloc(columns * sizeof (int)); } /* Code fo fill the array*/ return 1; } void
int allocate(int **array, unsigned int rows, unsigned int columns){
int i;
for (i = 0; i < rows; i++) {
array[i] = malloc(columns * sizeof (int));
}
/* Code fo fill the array*/
return 1;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++) {
free(v[i]);
}
free(v);
}
int main(int argc, char **argv) {
int rows, columns;
rows = atoi(argv[1]);
columns = atoi(argv[2]);
int *ipp[rows];
allocate(ipp, rows, columns);
de_allocate(ipp,rows);
return 0;
}
在分配功能结束时,ipp必须能够访问分配的2D阵列
分配函数这是正确的,但在de_分配函数中,我有一个SIGABRT信号问题是,您试图用代码
free(v)释放堆栈分配的var代码>
如果将指针数组定位错了位置,但在main
函数中使用int*ipp[rows]本地声明它,则可以这样做代码>
将其更改为int**ipp=malloc(sizeof(int*)*行)代码>如果您想保持de_分配不变
你可以用它来测试
#include <stdio.h>
#include <stdlib.h>
int allocate(int **array, unsigned int rows, unsigned int columns){
int i;
for (i = 0; i < rows; i++)
{
array[i] = malloc(columns * sizeof (int));
}
/* Code fo fill the array*/
return 1;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++)
{
free(v[i]);
}
free(v);
}
int main(int argc, char **argv)
{
int rows, columns;
int temp = 0;
rows = atoi(argv[1]);
columns = atoi(argv[2]);
int **ipp = malloc(sizeof(int*)*rows);
allocate(ipp, rows, columns);
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
ipp[i][j] = temp++;
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
printf("ipp[%d][%d] = %d\n", i, j, ipp[i][j]);
de_allocate(ipp,rows);
return 0;
}
#包括
#包括
整数分配(整数**数组、无符号整数行、无符号整数列){
int i;
对于(i=0;ivoid de_allocate)(int**v,无符号int行){
int i;
对于(i=0;i
这不是2D数组,只是对它的模拟。不要这样做,这样的代码应该只在博物馆中使用。使用真正的2D数组。如果它们比较大,不要在堆栈上分配它们,使用int(*ipp)[rows]=malloc(sizeof(double[rows][colums]);
分配和free(ipp)
来释放它和结束。没有必要使用来进行循环。因此,即使是你的函数也不再有什么用途了。
#include <stdio.h>
#include <stdlib.h>
int allocate(int **array, unsigned int rows, unsigned int columns){
int i;
for (i = 0; i < rows; i++)
{
array[i] = malloc(columns * sizeof (int));
}
/* Code fo fill the array*/
return 1;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++)
{
free(v[i]);
}
free(v);
}
int main(int argc, char **argv)
{
int rows, columns;
int temp = 0;
rows = atoi(argv[1]);
columns = atoi(argv[2]);
int **ipp = malloc(sizeof(int*)*rows);
allocate(ipp, rows, columns);
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
ipp[i][j] = temp++;
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
printf("ipp[%d][%d] = %d\n", i, j, ipp[i][j]);
de_allocate(ipp,rows);
return 0;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++) {
free(v[i]);
}
free(v);
-----^----
here you are attempting to free a variable for which you didn't dynamically allocate memory in the first place
}