[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArray: Problem allocating memory on stack on windows

From: Richard Frith-Macdonald
Subject: Re: NSArray: Problem allocating memory on stack on windows
Date: Thu, 9 Oct 2003 11:36:17 +0100

On Thursday, October 9, 2003, at 11:09 AM, Roland Schwingel wrote:


There is a problem with all the -initWithArray: and related methods.

On windows stacksize is limited. So at present when calling initWithArray with an array containing more then ~52100 elements stack runs out of space and applications crashes (or behaves really silly, depending what gets overwritten)

The solution here to fix this would be to replace all that dynamic array stuff
with malloc()/free() calls.

We always knew this was a potential problem ... but didn't really expect to run into it in practice (I guess the stack size on unix-like systems just tends to be a lot
bigger), so code like this only occasionally gets updated.

Simply replacing stack based memory management with heap based stuff is not a good enough solution ... it's very slow. Instead, what I do is a compromise ...

I select what seems a reasonable size (say a few thousand elements) and code such that below this size we allocate memory on the stack, and above it we use the heap. The rationale being that, where we are dealing with large enough amounts of data the overheads of heap management are proportionally smaller.

For instance, if -initWithArray: used malloc/free for an array containing just a few objects, it would perhaps double the time taken by the array creation/initialisation, but if it did it for an array containing 10000 objects, it would probably only
make a 1% difference.

Unfortunately, having two ways of dealing with the memory means more code, and more chance of bugs ... so generally when adopting this scheme I initially set the changeover point very small, so it's easy to exercise both schemes,
and only increase it when I'm confident both are working well.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]