Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!flaps From: flaps@utcsri.UUCP (Alan J Rosenthal) Newsgroups: comp.lang.c,comp.unix.questions Subject: Re: Question on large arrays in C Message-ID: <4124@utcsri.UUCP> Date: Fri, 13-Feb-87 00:19:44 EST Article-I.D.: utcsri.4124 Posted: Fri Feb 13 00:19:44 1987 Date-Received: Fri, 13-Feb-87 22:52:52 EST References: <1051@uwmacc.UUCP> Reply-To: flaps@utcsri.UUCP (Alan J Rosenthal) Organization: University of Toronto Lines: 47 Summary: In article <1051@uwmacc.UUCP> jwp@uwmacc.UUCP writes: >#include >#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@csri.toronto.edu CSNET: flaps@toronto BITNET: flaps at utorgpu