Re: [Dev-C++] run time errors with big arrays
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <wa...@wa...> - 2004-02-27 00:20:51
|
On 27 Feb 2004 at 0:16, risto holopainen wrote: > Initialising arrays of large size seems to be a problem - the programs > are compiled without warnings, but if the array (or multidimensional > array) exceeds some specific size, the program crashes. Has anyone of > you had the same problem, and is there anything to fix that? A related > question that I have not found answered in my programming textbooks: > How is a multidimensional array dynamically initialised? In other > words, this works: > double *ar; > ar = new double[10]; > but what is the correct syntax for initialising for example ar[10][10] > with new? The problem is that the C and C++ memory models are different. C++ sees something like double ar[10][10] and builds that on the stack. You quickly run out of space and start stepping on things that way. I had the same problem when I came from C to C++ (a short time ago.) So, you should either consider using vectors or set it up as a pointer to pointers (double **ar) and then initialize it like: // init 10 pointers to double. ar = new double *[10]; // init each pointer to double to a pointer to double. for (int i=0; i < 10; i++) ar[i] = new double[10]; At least... that's the way I've been doing it but I'm still new to C++. Oh, and be sure to delete them when you're done. HTH. Watson (the pencil neck) Davis |