C/C++中动态创建和释放二维数组的两种办法
2017-03-03 23:30
288 查看
// 动态创建nRow*nCol大小的二维数组,并初始化所有项为0
void MatrixConstruct(double ***pMatrix, int nRow, int nCol)
{
// 分配所有行的首地址
*pMatrix = (double **)malloc(sizeof(double *) * nRow);
for (int i = 0; i < nRow; i++)
{ // 按行分配每一列
(*pMatrix)[i] = (double *)malloc(sizeof(double) * nCol);
memset((*pMatrix)[i],0, sizeof(double) * nCol);
}
}
// 动态创建nRow*nCol大小的二维数组,并初始化所有项为0
double** MatrixConstruct(int nRow, int nCol)
{
double** matrix = NULL;
// 分配所有行的首地址
matrix = (double **)malloc(sizeof(double *) * nRow);
for (int i = 0; i < nRow; i++)
{ // 按行分配每一列
matrix[i] = (double *)malloc(sizeof(double) * nCol);
memset(matrix[i], 0, sizeof(double) * nCol);
}
return matrix;
}
// 释放动态创建的nRow行的二维数组空间
void MatrixDestroy(double **pMatrix, int nRow)
{
for (int i = 0; i < nRow; i++)
{
free((pMatrix)[i]);
pMatrix[i] = NULL;
}
a0cc
free(pMatrix);
}
int main()
{
int nRow = 10, nCol = 4;
double **matrix1 = MatrixConstruct(nRow, nCol);
MatrixDestroy(matrix1, nRow);
matrix1 = NULL;
double **matrix2 = NULL;
MatrixConstruct(&matrix2,nRow, nCol);
MatrixDestroy(matrix2, nRow);
matrix2 = NULL;
return 0;
}
void MatrixConstruct(double ***pMatrix, int nRow, int nCol)
{
// 分配所有行的首地址
*pMatrix = (double **)malloc(sizeof(double *) * nRow);
for (int i = 0; i < nRow; i++)
{ // 按行分配每一列
(*pMatrix)[i] = (double *)malloc(sizeof(double) * nCol);
memset((*pMatrix)[i],0, sizeof(double) * nCol);
}
}
// 动态创建nRow*nCol大小的二维数组,并初始化所有项为0
double** MatrixConstruct(int nRow, int nCol)
{
double** matrix = NULL;
// 分配所有行的首地址
matrix = (double **)malloc(sizeof(double *) * nRow);
for (int i = 0; i < nRow; i++)
{ // 按行分配每一列
matrix[i] = (double *)malloc(sizeof(double) * nCol);
memset(matrix[i], 0, sizeof(double) * nCol);
}
return matrix;
}
// 释放动态创建的nRow行的二维数组空间
void MatrixDestroy(double **pMatrix, int nRow)
{
for (int i = 0; i < nRow; i++)
{
free((pMatrix)[i]);
pMatrix[i] = NULL;
}
a0cc
free(pMatrix);
}
int main()
{
int nRow = 10, nCol = 4;
double **matrix1 = MatrixConstruct(nRow, nCol);
MatrixDestroy(matrix1, nRow);
matrix1 = NULL;
double **matrix2 = NULL;
MatrixConstruct(&matrix2,nRow, nCol);
MatrixDestroy(matrix2, nRow);
matrix2 = NULL;
return 0;
}
相关文章推荐
- c++动态二维数组创建与释放!
- c/c++ 动态创建与释放二维数组
- C++\V动态创建和释放二维数组
- C++版一维数组和二维数组的动态创建与释放
- 继续我的c++语法,,,二维数组的动态创建和释放
- 数据结构-C++描述:动态创建和释放一个二维数组
- C++动态创建二维数组,二维数组指针
- C++动态创建二维数组和清空cin缓冲
- c++动态创建二维数组
- 有关动态创建释放二维数组,以及指向数组的指针问题集合
- c\c++二维数组动态分配和释放
- C++ 关于二维数组的动态分配和释放——方法一(new)
- C++动态创建二维数组
- C++动态创建二维数组
- c++ 动态二维数组的创建和删除
- C++中二维数组的动态创建于处理(zzl
- C++动态创建二维数组和清空cin缓冲
- C++学习笔记(16)——动态二维数组的分配与释放
- 动态创建和释放二维数组
- 一个纠结的c++动态创建二维数组不能用的问题