Velocity Reviews > C++ > Length of Dynamically Created Multidemensional Arrays

Length of Dynamically Created Multidemensional Arrays

GraemeC
Guest
Posts: n/a

 05-01-2007
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;
}

Rolf Magnus
Guest
Posts: n/a

 05-01-2007
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
std::vector instead of an 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

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

pmouse
Guest
Posts: n/a

 05-02-2007
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