Question on large arrays in C

chris at mimsy.UUCP chris at mimsy.UUCP
Thu Feb 12 14:49:50 AEST 1987


In article <1051 at uwmacc.UUCP> jwp at uwmacc.UUCP (Jeffrey W Percival) writes:
>I am running 4.3BSD on a MicroVax II. ...

with array declarations that require 20480*5*8 = 819200 bytes.

>When I run it, I get "Segmentation violation".

>If I move the 5 array declarations up above main(),
>under the define statement, the program works OK.
>What is wrong with the program as listed above?

Nothing.  This is a `feature' of a large virtual address space with
demand paging.  The system cannot tell whether many stack pointer
alterations are proper, so it uses a heuristic.  If the stack
pointer has been moved less than 512 kilobytes, the stack allocation
is a controlled one and is allowed.  If it has moved by more than
512K, it is assumed to be accidental, and the program is sent a
SIGSEGV.

This heuristic has an obvious flaw.  To fix it, Berkeley made the
limit not really 512K, but rather the `stacksize' resource limit
for the process.  This is set to 512K by init, and inherited by
all children.  It can be changed with the setrlimit() system call,
or with the C shell's built-in `limit' command:

	limit stacksize 1m

will raise it to one megabyte;

	unlimit stacksize

will raise it to the kernel's configured maximum, probably 16M.

Incidentally, on all machines with which I have worked, large arrays
are more efficiently accessed when static than when on the stack.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list