Hello,
I am using this piece of code for allocating 3D array in memory by my dimensions HEIGHT, WIDTH, DEPTH which are variables.

``````p2DArray = new double**[HEIGHT];
for (int i = 0; i < HEIGHT; ++i) {
p2DArray[i] = new double*[WIDTH];

for (int j = 0; j < WIDTH; ++j)
p2DArray[i][j] = new double[DEPTH];
}``````

My problem is this: When i have for example HEIGHT = 512, WIDTH = 512 and DEPTH = 2 my 3D allocated array has right dimension in HEIGHT and WIDTH but DEPTH dimensions are not 2, but its larger.

For example when I want to read p2DArray[511][511][845646] it doesnt say me, that I am out of dimensions (with [845646] in DEPTH), but it will give me random number value. When I want to read on position for example p2DArray[518][511][0] it will say me, that I am out of dimension because of HEIGHT size.

Could you please tell me, why this piece of code allocates 3D array right in HEIGHT and WIDTH dimension, but false in DEPTH (it allocates a lot of bigger in DEPT dimension, than I would like)?

THANKS FOR ANY HELP!

## All 3 Replies

>>For example when I want to read p2DArray[511][511][845646] it doesnt say me that I am out of dimensions

Well duuh! the maximum is p2DArray[511][511][1]. c++ programs don't produce out-of-bounds error messages. They just crash and burn.

Problem is, that it doesn't "crash and burn", but it will normally run and write some random number on array position [511][511][845646], which is the problem i have.

Here is whole part of test code, which has this problem:

``````short int ***test;

int HEIGHT = 10;
int WIDTH = 20;
int DEPTH = 3;

test = new short int**[HEIGHT]; //allocating memory for arry of size 10 x 20 x 3
for (int x = 0; x < HEIGHT; ++x) {
test[x] = new short int*[WIDTH];
for (int y = 0; y < WIDTH; ++y)
test[x][y] = new short int[DEPTH];
}

for (int z = 0; z < DEPTH; z++) //filling it with ones
{
for (int y = 0; y < WIDTH; y++)
{
for (int x = 0; x < HEIGHT; x++)
{
test[x][y][z] = 1;
}
}
}

cout << test[5][15][2]; //it will write number one, its ok
cout << test[5][15][5]; //it should say, that i am off the array size with number 5 in depth dimension (program should throw exception), but it will write some random number -21598 and doesnt throw any exception, that i am looking out of allocated memory``````

>>cout << test[5][15][2]; //it will write number one, its ok

No it is not ok. 2 is out of bounds. only 0 and 1 are valid

>> cout << test[5][15][5]; //it should say, that i am off the array size with
// number 5 in depth dimension (program should throw exception), but it
// will write some random number -21598 and doesnt throw any exception,
//that i am looking out of allocated memory

It will not do any such thing as throw an exception at runtime because c/c++ has no bounds checks. That situation has been the thorn in every programmer's side since C language was invented and has been the cause of countless debugging hours. There are several tools like these to help with the debugging.

Try this if you want to see your program crash and burn `for (int z = 0; z < DEPTH+10; z++)`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.