C++ 中的二维动态内存分配数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14829105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2D dynamic memory allocation array in C++
提问by Casper
A few days ago I learned about creating 2D allocated memory arrays from the internet, it works perfect. To access the array we just simply use matrix[i][j]
, however is there any way that I can dereference this 2D array by using *
notation instead of []
for input as well as other methods?
几天前,我从互联网上了解了如何创建 2D 分配的内存数组,它完美无缺。要访问数组,我们只是简单地使用matrix[i][j]
,但是有什么方法可以通过使用*
符号而不是[]
输入以及其他方法来取消引用这个二维数组?
First questions is solved I can use *(*(matrix + i) + j)
第一个问题解决了我可以使用 *(*(matrix + i) + j)
Now I got another question, last code segment is to free the allocated memory (I got it from internet as well), but I don't understand it, why cant I just use delete [] matrix
?
现在我有另一个问题,最后一个代码段是释放分配的内存(我也是从互联网上得到的),但我不明白,为什么我不能使用delete [] matrix
?
int **matrix;
// dynamically allocate an array
matrix = new int *[row];
for (int count = 0; count < row; count++)
{
matrix[count] = new int[col];
}
// input element for matrix
cout << endl << "Now enter the element for the matrix...";
for (int i=0; i < row; i++)
{
for (int j=0; j < col; j++)
{
cout << endl << "Row " << (i+1) << " Col " << (j+1) << " :";
cin >> matrix[i][j]; // is there any equivalent declaration here?
}
}
// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
delete [] matrix[i] ;
}
delete [] matrix ;
采纳答案by Pubby
Since a[b]
is just *(a + b)
you can of course do this:
因为a[b]
只是*(a + b)
你当然可以这样做:
*(*(matrix + i) + j)
Anyway, those new
allocations are error prone. If one of the nested new
s throws then you'll have a leak. Try using std::vector
instead.
无论如何,这些new
分配很容易出错。如果其中一个嵌套new
s 抛出,那么您将有泄漏。尝试使用std::vector
。
回答by JBL
Answering your second question: when you allocate a 2D array with the following code
回答您的第二个问题:当您使用以下代码分配二维数组时
// dynamically allocate an array
matrix = new int *[row];
for (int count = 0; count < row; count++)
matrix[count] = new int[col];
you are in fact allocating one array of pointers (your matrix variable, which is a double pointer) and "row" arrays of integers (each one representing one row in your matrix, of size "col"), which are matrix[0]
, matrix[1]
, etc. up to matrix[row-1]
.
你实际上分配指针(您的矩阵的变量,它是一个双指针)和整数的“行”阵列中的一个阵列(每一个代表在矩阵的一行,大小“栏”),这是matrix[0]
,matrix[1]
等到matrix[row-1]
.
Thus, when you want to free your matrix, you'll first need to free every single row (the arrays allocated within the loop), and then the array which held the rows. In your case, the code you use to free your matrix is partly wrong, and should be more like the following :
因此,当您想释放矩阵时,首先需要释放每一行(循环内分配的数组),然后是保存行的数组。在您的情况下,您用来释放矩阵的代码部分错误,应该更像以下内容:
// free dynamically allocated memory
for( int i = 0 ; i < row ; i++ )
{
//first we delete each row
delete [] matrix[i] ;
}
//finally, we delete the array of pointers
delete [] matrix ;
The delete within the loop will free each row of your matrix, and the final delete will free the array of rows. In your code, you use delete row
times on your double pointer (matrix
), which makes no sense.
循环中的删除将释放矩阵的每一行,最终删除将释放行数组。在您的代码中,您row
在双指针 ( matrix
)上使用了删除时间,这是没有意义的。
Finally, using a single delete on the double pointer is wrong, because it would end up in a memory leak as you aren't freeing the memory allocated for each row, only the pointers referring to it.
最后,在双指针上使用单个删除是错误的,因为它最终会导致内存泄漏,因为您没有释放为每一行分配的内存,只有引用它的指针。
回答by SeedmanJ
Something like this would work:
像这样的事情会起作用:
int **matrix;
// dynamically allocate an array
matrix = new (std::nothrow) int *[row];
if (matrix == NULL)
{
// handle the error
}
for (int count = 0; count < row; count++)
{
*(matrix + count) = new (std::nothrow) int[col];
if (matrix[count] == NULL)
{
// handle the error
}
}
cout << "\nNow enter the element for the matrix...";
for (int i=0; i < row; i++)
{
for (int j=0; j < col; j++)
{
cout << "\nRow " << (i+1) << " Col " << (j+1) << " :";
cin >> *(*(matrix + i) + j);
}
}
回答by mlstudent
Yes, you use pointer addition, but you need to understand how the memory is laid out. Say x is a pointer to the first element of an array of ints, if you want to access x[2], you can use *(x+2). However, with matrices it can get quite confusing and you're a lot more likely to access wrong indices in your matrix if you do this, so I wouldn't advise it.
是的,您使用指针加法,但您需要了解内存是如何布局的。假设 x 是一个指向整数数组第一个元素的指针,如果要访问 x[2],可以使用 *(x+2)。但是,对于矩阵,它可能会变得非常混乱,如果这样做,您更有可能访问矩阵中的错误索引,因此我不建议这样做。
回答by Victor Zamanian
You could do *(*(matrix+i)+j)
. It should be equivalent to bracket notation. What is happening with both notations is simply pointer arithmetic.
你可以做*(*(matrix+i)+j)
。它应该等同于括号表示法。两种表示法发生的事情只是指针算术。