Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我有一个指向指针数组的指针!
int **matrixPtr; matrixPtr = new int*[5]; for(i=0; i<5; ++i){ matrixPtr[i]= new int[5]; }
我想知道这是否是释放内存的正确方法!
for(i=0; i<5; ++i){ delete [] matrixPtr[i]; } delete [] matrixPtr;
谢谢!
没问题。这是正确的!您以与分配相反的顺序解除分配!我什至认为没有其他方法可以做到这一点。
是和不是。是的,如果您必须像以前那样手动分配内存,这是手动释放内存的正确方法。
但是不,您应该避免手动分配和释放内存。如果您坚持使用 C++03 并且没有任何智能指针,则应该使用向量向量。在 C++11 中,您有更多选项,即智能指针和std::array,后者仅在您知道编译时内部或外部维度或两者的大小时。在 C++14std::dynarray中也可以成为一种选择。
std::array
std::dynarray