Syntax for dynamically highlighting a two-dimensional array of smart pointers

I need to dynamically allocate a two-dimensional array of smart pointers, but the syntax for it confuses me. I need this to be dynamic:

std::unique_ptr<someClass> myArray[size1][size2];

So, from what I understand, I am creating a pointer to a pointer to a type:

someClass** myArray; //actaully the type is std::unique_ptr<someClass> but I'll just keep it simple

Then, to highlight it, I:

myArray* = new someClass*[size1];
for(int i = 0; i < size1; i++)
    myArray[i] = new someClass[size2];

But this does not use smart pointers, which means that I will have to manually delete it later, and I don’t know how to make these pointers smart pointers;

Type std :: unique_ptr, but then I need a pointer to a pointer to a type, so I tried:

std::unique_ptr<std::unique_ptr<std::unique_ptr<someClass>>> myArray;

But after that, I lost the way I extracted it. Can someone please help me?

+5
source share
2

, , .

, , , . C ++ "typedef". , , : , .. Unique_ptr, , typedef , . , , .

, , , , . ++ 11, "using": http://en.cppreference.com/w/cpp/language/type_alias. , !

. "test_dynamic_2darray1" 10x10. , 100 , .

size_t destructor_count = 0;
class MyClass {
    public:
    ~MyClass() {
        std::cout << "Destructor call #" << ++destructor_count << std::endl;
    }
};

typedef std::unique_ptr<MyClass[]> ManagedC;

void test_dynamic_2darray1() {
    size_t dimension1 = 10, dimension2 = 10;

    auto managed_array = std::unique_ptr<ManagedC[]>(new ManagedC[dimension1]);
    for (size_t i = 0; i < dimension1; ++i)
        managed_array[i] = ManagedC(new MyClass[dimension2]);
}

, , :

void test_dynamic_2darray2() {
    size_t dimension1 = 10, dimension2 = 10;

    auto simple_array = new MyClass*[dimension1];
    for (size_t i = 0; i < dimension1; ++i)
        simple_array[i] = new MyClass[dimension2];
}

, !:) , , - ! , : http://frankriesecodingblog.blogspot.com/2015/01/performance-of-dynamic-multi.html. , .

, : int . , , , . , size_t. ? , 64- "int" 32 , , size_t, 64 . int , 32- 64- . , , , , , ptrdiff_t.

+3

, ++ modern 2D int ( 3 5) (unique_ptr) , make_unique move().

unique_ptr<unique_ptr<int[]>[]>     smartPtr2D;
unique_ptr<int[]>                   smartPtr1D;

int dataCounter = 0;

smartPtr2D = make_unique< unique_ptr<int[]>[] >(3);
for (int i = 0; i<3; i++)
{
    smartPtr1D = make_unique<int[]>(5);
    for (int j = 0; j<5; j++)
    {
        smartPtr1D[j] = dataCounter;
        dataCounter++;
    }
    smartPtr2D[i] = move(smartPtr1D);
}
+2

All Articles