Search code examples
c++arrayspointersmultidimensional-arraydynamic-arrays

Deleting 3d Dynamic array in c++


I made a function to initialize a dynamic 3d array

void initialize_array_3d(char ***Array, int size1, int size2, int size3)
{
    Array = new char**[size1];
    for (int t=0; t<size1+1; t++)
    {
        Array[t] = new char *[size2];
        for (int r=0; r<size2+1; r++)
        {
            Array[t][r] = new char [size3];
        }
    }
}

And it works fine but when I try to delete it with a function

void deinitialize_array_3d(char ***Array, int size1, int size2)
{
    for (int t=0; t<size1+1; t++)
    {
        for (int r=0; r<size2+1; r++)
        {
            delete[] Array[t][r];
        }
        delete[] Array[t];
    }
    delete[] Array;
}

My program crashes, why? and how do i fix it?

In main:

int main()
{
    char ***Failed;
    initialize_array_3d(Failed, 5, 4, 2);
    deinitialize_array_3d(Failed, 5, 4);
    Failed = nullptr;
    return 0;
}

Solution

  • For starters function initialize_array_3d is wrong. It should look at least the following way

    void initialize_array_3d( char ****Array, int size1, int size2, int size3 )
                              ^^^^^^^^^^^^^^
    {
        *Array = new char**[size1];
    
        for ( int t = 0; t < size1; t++ )
                         ^^^^^^^^^
        {
            ( *Array )[t] = new char *[size2];
    
            for ( int r = 0; r < size2; r++ )
                             ^^^^^^^^^
            {
                ( *Array )[t][r] = new char [size3];
            }
        }
    }
    

    That is the first parameter should be passed indirectly using pointer.

    A function call might look like

    initialize_array_3d( &Failed, 5, 4, 2);
                         ^^^^^^^
    

    Otherwise parameter char ***Array is a local variable of the function and the function deals with a copy of the argument. Any changes of the local variable do not influence on the original argument.

    Or you could declare the parameter as reference to the pojnter. For example

    void initialize_array_3d( char *** &Array, int size1, int size2, int size3 )
                              ^^^^^^^^^^^^^^
    {
        Array = new char**[size1];
    
        for ( int t = 0; t < size1; t++ )
                         ^^^^^^^^^
        {
            Array[t] = new char *[size2];
    
            for ( int r = 0; r < size2; r++ )
                             ^^^^^^^^^
            {
                Array[t][r] = new char [size3];
            }
        }
    }
    

    In this case the function call might look like

    initialize_array_3d( Failed, 5, 4, 2);
                         ^^^^^^  
    

    As for function deinitialize_array_3d then it should look like

    void deinitialize_array_3d(char ***Array, int size1, int size2)
    {
        for ( int t = 0; t < size1; t++ )
                         ^^^^^^^^^ 
        {
            for ( int r = 0; r < size2; r++ )
                             ^^^^^^^^^
            {
                delete[] Array[t][r];
            }
            delete[] Array[t];
        }
    
        delete[] Array;
    }