What different memory model?
The memory layout of an array is identical between C and C++. The only
difference is that when dynamically allocating, you normally use
new/delete or new[]/delete[] since they are type-safe and will call
constructors/destructors.
Note that depending on compiler and environment, large objects on the
stack may create problems. Some environments allows the stack to grow by
use of guard pages, i.e. the stack is only allocated a page at a time, and
at the end of the stack is an extra page which is basically
write-protected. If the program tries to write to this page, an exception
is generated and the OS will then notice that it was the guard page that
was accessed. It will then change the guard page to a normall read/write
page and then allocate a new guard page directly after the previous guard
page. The advantage of this concept is that the system doesn't have to
statically allocate huge stacks that may never be used. The disadvantage
is that if a program allocates a very large array and doesn't access it in
increasing order, the program may make an access AFTER the guard page,
with a failure as a result. Some compilers have an option to add extra
code to step-by step grow the stack in case a function has more
auto-variables than what fits in a memory page.
Let's say the machine has a page size of 4kB and dynamically grows the
stack using guard pages. The following function will then fail unless the
compiler adds extra code:
void foobar() {
char array[4096];
int var;
// This stack access makes a jump of >= 4kB and may totally miss
// the guard page in case the array variable was placed on the
// first byte of the guard page.
var = 10;
}
/Per W
On Thu, 26 Feb 2004 wa...@wa... wrote:
> 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
>
>
>
> -------------------------------------------------------
> SF.Net is sponsored by: Speed Start Your Linux Apps Now.
> Build and deploy apps & Web services for Linux with
> a free DVD software kit from IBM. Click Now!
> http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|