3D array not working

Im trying to store multiplication in a 3D array allocated through pointers (a practice problem in a book). I can get the 3D array but I cant store the multiplication in the pointer array (i guess you can say) in the for loop of the function "three dimensional array" any ideas why? When i comment out the whole for loop it works fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

/**
Function that makes a three dimensional array
with its argurment using a pointer and multiplies
numbers in a loop for its values and stores it
in the array of pointers
**/

void ***three_dimensional_array (int ***arg1)
{

    for (int i = 0; i < 9; ++i)
        {
            arg1[i] = new int*[9];
        }
   for (int i = 0; i < 9; ++i)
        {
            for (int j = 0; j < 9; ++j)
                {
                    for (int k = 0; k < 9; ++k)
                    {
                        arg1[i][j][k] = i * j * k; // stores multiplication in 3DI ARRAY
                        cout << arg1[i][j][k] << '\t';
                    }
                }
        }
  delete[] arg1;
}


// call the function in main

int main()
{
    int ***three_p_free_store = new int**[9];
    three_dimensional_array(three_p_free_store);
}
Where is arg1[i][j][k] pointing?
Your array shall have i * j * k ( 9 * 9 * 9 ) elements.
You allocated only i * j ( 9 * 9 ) elements. The firs i ( 9 ) elements you allocated with the statement

int ***three_p_free_store = new int**[9];

and the next j ( 9 ) elements for each i you allocated with the statement

for (int i = 0; i < 9; ++i)
{
arg1[i] = new int*[9];
}


So now you need to allocate the next k ( 9 ) elements for each i * j.

Last edited on
a practice problem in a book

What kind of book would that be? What i'd be hoping to see in a C++ book would be "here you have C-style arrays. They exist. Now let's move on to std::vector and std::array)". If you have 2 dimensional C style arrays in your code, something is fishy. If you get to 3, something is very, very wrong in your code.

hanst99 (2757)

Now let's move on to std::vector and std::array)".


Now it is very interesting to me. I would like to know did you use std::array with multidimensional arrrays?!:) Tell us how std::array is convient to use with multidimensional arrrays.:)
If the dimensions are fixed std::array works fine as multidimensional array.

std::array<std::array<std::array<int, 9>, 9>, 9> three_dimensional_array;

@Peter87
If the dimensions are fixed std::array works fine as multidimensional array.

std::array<std::array<std::array<int, 9>, 9>, 9> three_dimensional_array;


It is not a working with multidimensional array. It is a declaration from which it is not clear which subscriptor is the first, which is the second and so on. Show for example a construction with std::array that corresponds to alement record of a usual array as

a[1][2][3];
Last edited on
sorry i couldnt reply sooner, the website was down. thanks for all your help. Vlad From Moscow, your idea worked and now the array is able to be used. Thank you.
Topic archived. No new replies allowed.