# Length of Dynamically Created Multidemensional Arrays

Discussion in 'C++' started by GraemeC, May 1, 2007.

1. ### GraemeCGuest

How can you determine the length of a dynamically created
multidimensional array?

Tried using sizeof unsuccesufully. It always returns 4, presumably
becuase I am using ints and it is just telling me the size of the
pointer myArray

Many thanks

G

int ** myArray = 0;
int Rows;
int Cols;

cout << "Number of Rows?" << endl;
cin >> Rows;

myArray = new int*[Rows];

for (int i_r = 0; i_r < Rows; i_r++)
{
cout << "Number of cols?" << endl;
cin >> Cols;

myArray[i_r] = new int[Cols];
for (int i_c=0; i_c < Cols; i_c++) {
cout << "Number" << i_c << endl;
cin >> myArray[i_r][i_c];
}
}
}

return 0;
}

GraemeC, May 1, 2007

2. ### Rolf MagnusGuest

GraemeC wrote:

> How can you determine the length of a dynamically created
> multidimensional array?

By remembering the size you specified when creating it. Alternatively, use

> Tried using sizeof unsuccesufully. It always returns 4, presumably
> becuase I am using ints and it is just telling me the size of the
> pointer myArray

It has nothing to do with int. If you use sizeof on a pointer, you get the
size of a pointer.

Rolf Magnus, May 1, 2007

3. ### pmouseGuest

To put it simply: You can't.

Therefore, you either store the length somewhere, or you put a marker
at the boundaries of the array.

look for STL's vector type.

Regards,

PQ

pmouse, May 2, 2007