Follow-up Comment #14, bug #18087 (project freeciv):

>> Oh there is so much stack abuse in freeciv.
> ...question: is allocating huge arrays on the stack instead of 
> allocating the same arrays on the heap (in the absence of 
> recursion etc) really an abuse?
Having read around a bit more, I'm now convinced it's not a good idea.
Wikipedia <>
suggests that "a thread's stack size can be as small as a few dozen
kilobytes", and more specifically, the Windows documentation
<> says "The
default stack reservation size used by the linker is 1 MB."

What I think I'd missed with my "it's all memory" argument is that when
memory runs out, malloc() can return NULL (from which the application can
theoretically recover), whereas allocating memory on the stack can't fail, so
the OS has little option but to kill the entire app. It looks like as a result
of that, some (all?) OSes (such as Windows, see previous article) set aside
memory for app stacks, so that apps aren't killed for the crime of calling a
function when things get tight. Thus stack memory is more reliable than heap
memory, thus more precious, thus OSes place limits on it, thus it's not a good
idea to try to allocate arbitrarily large chunks of it.

Sorry if that was obvious to everyone else. Conclusion is, we shouldn't
allocate O(xsize*ysize) memory on the stack. IMO we should take the
"stack-abuse" changes on S2_3, as well as the assign_continent_flood() change
that we know we need. I've had a look and they look basically fine to me; more
in future comments.


Reply to this item at:


  Message sent via/by Gna!

Freeciv-dev mailing list

Reply via email to