如何解决如何在C ++中使用指针动态分配和取消分配2D数组?
我想使用指针动态分配然后释放2D数组。 我的想法是创建一个指向指针数组的指针,其中每个指针都指向一个int,这就是我想要的方式。 这些函数是布尔类型的,因为无论操作是否成功,我都想返回一个布尔值。
问题在于deallocateTwoDimTable
函数不会返回布尔值,因此似乎根本不起作用。如果我不使用int** table = nullptr;
,而未初始化int** table
,那么我会收到错误消息(取消分配时),我要释放的内存尚未初始化。
如果将参数int **table
更改为int **&table
,代码确实可以工作,但是我想这意味着按引用传递而不是使用指针。我该如何运作?非常感谢所有帮助:)
这是我的主要方法:
int main() {
int** table = nullptr;
int sizeX = 5; // rows
int sizeY = 3; // columns
bool allocationResult = allocateTwoDimTable(table,sizeX,sizeY);
cout << endl << "allocation result: " << (allocationResult ? "true" : "false") << endl;
bool deallocationResult = deallocateTwoDimTable(table,sizeX);
cout << endl << "deallocation result: " << (deallocationResult ? "true" : "false");
}
分配功能:
bool allocateTwoDimTable(int **table,int sizeX,int sizeY) {
if (sizeX <= 0 || sizeY <= 0) return false;
// allocate 2D table
table = new int*[sizeX];
for(int i = 0; i < sizeX; i++) {
table[i] = new int[sizeY];
}
cout << endl << "Table " << sizeX << " x " << sizeY << endl;
for(int i = 0; i < sizeX; i++) {
cout << endl;
for(int j = 0; j < sizeY; j++) {
cout << table[i][j] << ",";
}
}
return true;
}
解除分配功能:
bool deallocateTwoDimTable(int **table,int sizeX) {
if(sizeX <= 0) return false;
for (int k = 0; k < sizeX; k++) {
delete[] table[k];
}
delete[] table;
return true;
}
解决方法
您必须通过引用传递指针。否则,函数allocateTwoDimTable将处理原始指针的值的副本。也就是说,更改副本不会影响原始指针。
参数sizeX
和sizeY
的类型也应为size_t
,因为通常情况下,类型int
不能指定所需的数组维数。
可以像这样声明函数
bool allocateTwoDimTable( int ** &table,size_t sizeX,size_t sizeY);
请注意,您将创建未初始化的数组
table[i] = new int[sizeY]
所以输出它们
cout << table[i][j] << ",";
调用未定义的行为。
,当您需要传递引用或通过指针传递时,如果有机会,最好通过引用传递。
指针可以带有空参数,引用不能,使用起来更安全,除非出于某种原因,您特别需要传递空参数(我认为并非如此)。
请注意,打印阵列时您正在访问未初始化的内存。
还要注意,尽管使用布尔标志来表示分配错误,但您仍然很容易受到攻击,但更好的方法是使用std::exception
。
bool allocateTwoDimTable(int** &table,int sizeX,int sizeY) {
if (sizeX <= 0 || sizeY <= 0) return false;
// allocate 2D table
table = new int* [sizeX];
for (int i = 0; i < sizeX; i++) {
table[i] = new int[sizeY];
}
cout << endl << "Table " << sizeX << " x " << sizeY << endl;
for (int i = 0; i < sizeX; i++) {
cout << endl;
for (int j = 0; j < sizeY; j++) {
table[i][j] = i + j; //initializing elements of the array
cout << table[i][j] << ",";
}
}
return true;
}
bool deallocateTwoDimTable(int (** &table),int sizeX) {
if (sizeX <= 0) return false;
for (int k = 0; k < sizeX; k++) {
delete[] table[k];
}
delete[] table;
return true;
}
int main() {
int** table = nullptr;
const int sizeX = 5; // rows
const int sizeY = 3; // columns
try {
const bool allocationResult = allocateTwoDimTable(table,sizeX,sizeY);
cout << endl << "allocation result: " << (allocationResult ? "true" : "false") << endl;
const bool deallocationResult = deallocateTwoDimTable(table,sizeX);
cout << endl << "deallocation result: " << (deallocationResult ? "true" : "false");
}
catch (std::exception& e)
{
std::cout << e.what();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
,
看看这个答案https://stackoverflow.com/a/936709/9936992,他们还提到可以使用一个模拟两个D数组的单个数组
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。