Re: [Dev-C++] run time errors with big arrays
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Nicolas R. <ni...@bo...> - 2004-02-27 00:04:47
|
> How is a multidimensional array dynamically initialised? In other=20
> words [...]
> what is the correct syntax for initialising for example ar[10][10]=20
> with new?
That syntax will work with static variables, i.e.
int arr[10][10];
But it won't work at all with new. You will need to declare the array=20
as if it were one-dimensional, i.e.
int* arr =3D new int[num_rows * num_cols];
and then do the math "by hand" when accessing elements, e.g.
arr[x * num_cols + y] =3D foo;
I can already hear you screaming "but that totally sucks! the language=20=
should provide a better way to do it!", but before you do so, please=20
remember that arrays are and have always been very low-level in C and=20
C++. If you want nicer syntax, all you have to do is write a Matrix=20
class to encapsulate the array, or even better, get one of the several=20=
excellent ones that already exist out there.
For a more thorough discussion of this issue, check out the C++ FAQ=20
Lite:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html
paragraphs [16.15] and [16.16].
By the way, is googling for 'multidimensional arrays C++' that hard? :-)
Regards,
Nicol=E1s
|