Question on large arrays in C
Alan J Rosenthal
flaps at utcsri.UUCP
Fri Feb 13 15:19:44 AEST 1987
In article <1051 at uwmacc.UUCP> jwp at uwmacc.UUCP writes:
>#include <stdio.h>
>#define N 20480
>main()
>{
> double x[N];
> double y_1[N];
> double y_2[N];
> double y_3[N];
> double y_4[N];
> char *l = "";
>
> fprintf(stdout, "hi\n");
> exit(0);
>}
>----------
>
>When I run it, I get "Segmentation violation".
>dbx reports the violation to occur on the "char *l" line.
>If I move the 5 array declarations up above main(),
>under the define statement, the program works OK.
Apparently you are overrunning the stack area (where auto variables are
often (and apparently, in this case) allocated), causing a segmentation
exception. If you move the five double declarations "up above main()",
they are no longer auto variables and thus are no longer allocated on the
stack.
A more appropriate way to do this which continues to keep the scope of those
variables restricted to the main() function is to declare them as static,
in the same place as they are declared above, like:
static double x[N];
, but this has radically different semantics in the case of a recursive
function call - namely, that the different function invocations will share
these variables rather than having private versions. Hopefully your program
doesn't require this feature of auto variables, in which case inserting
'static' should solve your problem.
--
Alan J Rosenthal
UUCP: {backbone}!seismo!mnetor!utgpu!flaps, ubc-vision!utai!utgpu!flaps,
or utzoo!utgpu!flaps (among other possibilities)
ARPA: flaps at csri.toronto.edu
CSNET: flaps at toronto
BITNET: flaps at utorgpu
More information about the Comp.unix.questions
mailing list