一、C++二维数组 - 动态分配、释放
1、第一维、第二维均未知
#include <iostream>
#include <memory.h>
using namespace std;
int main()
{
int rows, cols;
cout << "请输入二维数组行数:";
cin >> rows;
cout << "请输入二维数组列数:";
cin >> cols;
int** a = new int* [rows];
for (int i = 0; i < rows; i++)
{
a[i] = new int [cols];
}
for (int i = 0; i < rows; i++)
{
memset(a[i], 0, cols * sizeof(int));
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < rows; i++)
{
delete [] a[i];
}
delete [] a;
return 0;
}
2、第一维已知
#include <iostream>
#include <memory.h>
using namespace std;
const int rows = 3;
int main()
{
int cols;
cout << "请输入二维数组列数:";
cin >> cols;
int* a[rows];
for (int i = 0; i < rows; i++)
{
a[i] = new int[cols];
}
for (int i = 0; i < rows; i++)
{
memset(a[i], 0, cols * sizeof(int));
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < rows; i++)
{
delete [] a[i];
}
return 0;
}
3、第一维已知(内存连续)
#include <iostream>
#include <memory.h>
using namespace std;
const int rows = 3;
int main()
{
int cols;
cout << "请输入二维数组列数:";
cin >> cols;
int* a[rows];
a[0] = new int [rows * cols];
for (int i = 1; i < rows; i++)
{
a[i] = a[i - 1] + cols;
}
memset(a[0], 0, rows * cols * sizeof(int));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
delete [] a[0];
return 0;
}
4、第二维已知(内存连续)
#include <iostream>
#include <memory.h>
using namespace std;
const int cols = 9;
int main()
{
int rows;
cout << "请输入二维数组行数:";
cin >> rows;
int (*a)[cols];
a = new int [rows][cols];
memset(a, 0, rows * cols * sizeof(int));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
delete [] a;
return 0;
}
二、C二维数组 - 动态分配、释放
1、第一维、第二维均未知
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int main()
{
int rows, cols;
printf("请输入二维数组行数:");
scanf("%d", &rows);
printf("请输入二维数组列数:");
scanf("%d", &cols);
int** a = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++)
{
a[i] = (int*)malloc(cols);
}
for (int i = 0; i < rows; i++)
{
memset(a[i], 0, cols * sizeof(int));
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++)
{
free(a[i]);
}
free(a);
return 0;
}
2、第一维已知
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
const int rows = 3;
int main()
{
int cols;
printf("请输入二维数组列数:");
scanf("%d", &cols);
int* a[rows];
for (int i = 0; i < rows; i++)
{
a[i] = (int*)malloc(cols * sizeof(int));
}
for (int i = 0; i < rows; i++)
{
memset(a[i], 0, cols * sizeof(int));
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++)
{
free(a[i]);
}
return 0;
}
3、第一维已知(内存连续)
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
const int rows = 3;
int main()
{
int cols;
printf("请输入二维数组列数:");
scanf("%d", &cols);
int* a[rows];
a[0] = (int*)malloc(rows * cols * sizeof(int));
for (int i = 1; i < rows; i++)
{
a[i] = a[i - 1] + cols;
}
memset(a[0], 0, rows * cols * sizeof(int));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
free(a[0]);
return 0;
}
4、第二维已知(内存连续)
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
const int cols = 9;
int main()
{
int rows;
printf("请输入二维数组行数:");
scanf("%d", &rows);
int(*a)[cols];
a = (int(*)[cols])malloc(rows * cols * sizeof(int));
memset(a, 0, rows * cols * sizeof(int));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
free(a);
return 0;
}
三、C++三维数组 - 动态分配、释放
第一维、第二维、第三维均未知
#include <iostream>
#include <memory.h>
using namespace std;
int main()
{
int x, y, z;
cout << "请输入三维数组第一维长度:";
cin >> x;
cout << "请输入三维数组第二维长度:";
cin >> y;
cout << "请输入三维数组第三维长度:";
cin >> z;
int*** a = new int** [x];
for (int i = 0; i < x; i++)
{
a[i] = new int* [y];
for (int j = 0; j < y; j++)
{
a[i][j] = new int [z];
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
memset(a[i][j], 0, z * sizeof(int));
}
}
for (int i = 0; i < x; i++)
{
cout << "矩阵" << i + 1 << ":" << endl;
for (int j = 0; j < y; j++)
{
for (int k = 0; k < z; k++)
{
cout << a[i][j][k] << " ";
}
cout << endl;
}
cout << endl;
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
delete [] a[i][j];
}
delete [] a[i];
}
delete [] a;
return 0;
}
四、C三维数组 - 动态分配、释放
第一维、第二维、第三维均未知
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int main()
{
int x, y, z;
printf("请输入三维数组第一维长度:");
scanf("%d", &x);
printf("请输入三维数组第二维长度:");
scanf("%d", &y);
printf("请输入三维数组第三维长度:");
scanf("%d", &z);
int*** a = (int***)malloc(x * sizeof(int**));
for (int i = 0; i < x; i++)
{
a[i] = (int**)malloc(y * sizeof(int*));
for (int j = 0; j < y; j++)
{
a[i][j] = (int*)malloc(z * sizeof(int));
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
memset(a[i][j], 0, z * sizeof(int));
}
}
for (int i = 0; i < x; i++)
{
printf("矩阵%d:\n", i + 1);
for (int j = 0; j < y; j++)
{
for (int k = 0; k < z; k++)
{
printf("%d ", a[i][j][k]);
}
printf("\n");
}
printf("\n");
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
free(a[i][j]);
}
free(a[i]);
}
free(a);
return 0;
}
参考:
C与C++中二维数组的动态分配内存方法
C++中的delete和delete[]的区别