Dynamic 3 Dimensional Array Crash

The program crashes on the last loop right after J is updated to the value of one.
Why does this not work? How can J being updated to 1 impact the array?

It must have something to do with how I declared the pointers, but I can't quite figure it out..

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
41
42
43
44
45
#include <iostream>

using namespace std;

void lhwArray(int length, int height, int width)
{
    int ***p_p_p_array;
    p_p_p_array = new int**[3];
    for (int i = 0; i < 3; i++)
    {
        *p_p_p_array = new int*[3];
    }
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            **p_p_p_array = new int[3];
        }
    }
    cout << p_p_p_array[0][0][0];
    cout << "Made one way";

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 3; k++)
            {
                p_p_p_array[i][j][k] = i * j * k;
            }
        }
    }
    cout << "Made it all the way.";
    cout << p_p_p_array[1][2][3] << endl;
}

int main()
{
    int length = 0;
    int height = 0;
    int width = 0;
    lhwArray(length,height,width);
}

Last edited on
Line 11, you're just re-assigning p_p_p_array 3 times in a row (and also created more memory leaks each time). I believe you want to be indexing [i] there. Same for line 17, it would seem.
Why write code like that?
I've got it working now with parameters and all. I was so focused on the pointers that I forgot to index! Rather ashamed I spent an hour trying to figure out something so simple , rofl. Thank you for the help Ganado.

Edit: Shawnlau, the silly pointers was me trying to figure out how to make it work
Last edited on
Mike, pay me no nevermind. I'm getting back into code to try to keep my mind sharp. But some things just make me glaze over :) I mean nothing buy it.
Topic archived. No new replies allowed.