//dynamically assign int array M * N
//use C
int **array = NULL;
array = (int **)malloc(M * sizeof(int *));
for(int i = 0; i < M; ++i)
{
array[i] = (int *)malloc(N * sizeof(int));
}
//free the malloced memory
for(int i = 0; i < M; ++i)
{
free(array[i]);
}
free(array);
//use C++
int **array = NULL;
array = new int *M;
for(int i = 0; i < M; ++i)
{
array[i] = new int[N];
}
//delete the new memory
for(int i = 0; i < M; ++i)
{
delete [] array[i];
}
delete []array;
c/c++ 动态创建与释放二维数组
最新推荐文章于 2022-11-23 17:06:59 发布