Allocate memory for 2D array with C++ new[]

后端 未结 8 1382
情深已故
情深已故 2020-12-22 01:04

When I read some values from the user and I need to create an array of the specific size I do it somehow like this:

#include 
using namespace         


        
8条回答
  •  暖寄归人
    2020-12-22 01:24

    unsigned **numbers;
    numbers = new unsigned*[a];
    for (int i=0; i

    It doesn't behave exactly like a 2d array, in that it's not contiguous in memory, but it will do. You can also use the notation numbers[i][j] to access elements.

    Remember to delete[] each element of numbers at the end before delete[]-ing numbers itself

    Using std::vector is probably a preferable solution, as detailed in other posts

提交回复
热议问题