【发布时间】:2016-10-11 03:34:33
【问题描述】:
我是 C++ 新手,无法理解这段代码。我把我目前的理解放在了 cmets 中。这是思考这个问题的正确方式吗?如果没有,正在做什么? (简单来说)
int** arrayA; //a pointer to a pointer
arrayA = new int* [2]; //Does this 2 mean that there will be 2 rows?
arrayA[0] = new int[3]; //for the first row, three columns
arrayA[1] = new int[3]; //for the second row, three columns
/*Are the two lines above this CREATING or just ACCESSING the array? */
如果我的数组有 3 行,我是否必须添加 arrayA[2] = new int[3];?
我知道我也必须删除它,因为它是动态分配的,否则堆中会出现内存泄漏错误。正确的?
【问题讨论】:
-
表示2行3列。他们将被访问为
arrayA[row_number][column_number] -
//2 pointers:错了。只有一个指针。 -
@Jaden,表示指向指针的指针。
-
您所拥有的称为锯齿状数组,具有手动内存管理功能。而是使用a single
std:.vectoras storage 作为数组。 -
@Jaden,你的理解是准确的。回答中没有什么可说的了。
标签: c++ arrays dynamic-memory-allocation