在C++中,如果想要申请动态数组,必须要用动态分配的方式。
int **matrix=new int*[num_of_rows];
for(int i=0;i<num_of_rows;i++){
int *row=new int[num_of_cols];
//在这里对这一行进行赋值
for(int j=0;j<num_of_cols;j++){
row[j]=j;
}
matrix[i]=row;
}
这样,就实现了一个二维数组空间的申请,以及赋值。
但是在实际使用中,可能会因为动态申请的空间过大,系统在申请空间时失败。此时会抛出Exception: fail to allocate memory block。那么,我们需要用一种方法告诉系统,如果申请空间失败,