From owner-freebsd-questions@FreeBSD.ORG Thu May 6 14:53:01 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8B37016A4CE for ; Thu, 6 May 2004 14:53:01 -0700 (PDT) Received: from mail.broadpark.no (mail.broadpark.no [217.13.4.2]) by mx1.FreeBSD.org (Postfix) with ESMTP id 25CCD43D4C for ; Thu, 6 May 2004 14:53:00 -0700 (PDT) (envelope-from henrik.w.lund@broadpark.no) Received: from broadpark.no (52.80-202-129.nextgentel.com [80.202.129.52]) by mail.broadpark.no (Postfix) with ESMTP id D2C62146A; Thu, 6 May 2004 23:53:10 +0200 (MEST) Message-ID: <409AA757.7020708@broadpark.no> Date: Thu, 06 May 2004 23:00:07 +0200 From: Henrik W Lund User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.6) Gecko/20040410 X-Accept-Language: no, en-us, en MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <20040506190033.A45C616A5CF@hub.freebsd.org> In-Reply-To: <20040506190033.A45C616A5CF@hub.freebsd.org> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit cc: ckorves@hotmail.com Subject: Re: segmentation fault-- is my array too long? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 May 2004 21:53:01 -0000 > ------------------------------------------------------------------------ > > Subject: > segmentation fault-- is my array too long? > From: > "Caroline Korves" > Date: > Thu, 06 May 2004 14:58:43 -0400 > To: > freebsd-questions@freebsd.org > > To: > freebsd-questions@freebsd.org > > > Hello, > > This short program below represents a problem I am having with > segmentation faults in a much larger C program that has numerous > arrays. Seems as though when I increase the number of elements in an > array (here, for example, beyond 130,000) a seg fault occurs. > > Any idea on what I should change to make the program run with large > numbers of elements in my arrays? > > Thanks. > #include > #include > #include > > #define td 365 /* # days in trxn season */ > #define lifetab 94 /* enter 94 lines from life > table, corresponds to 27-120 years */ > /* # persons in run */ > #define persons 150000 > #define scens 4 > > int main() > { > long int j, person=0; > > double ncost[persons][scens]; > double nuts[persons][scens]; > > printf("check "); printf("\n"); > > for (person=0; person { > ncost[person][0]=0.00; > ncost[person][1]=0.00; > ncost[person][2]=0.00; > ncost[person][3]=0.00; > nuts[person][0]=0.00; > nuts[person][1]=0.00; > nuts[person][2]=0.00; > nuts[person][3]=0.00; > } > > > printf("persons "); printf("%d\n", persons); > > return 0; > > } > _________________________________________________________________ > > [1]FREE pop-up blocking with the new MSN Toolbar get it now! > > References > > 1. http://g.msn.com/8HMAENUS/2728??PS=47575 > > > ------------------------------------------------------------------------ > > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" Greetings, Caroline. It could be that your array is too long. I managed to run the program (straight copy and paste), but it could be that your program isn't allocated enough memory initially to hold those two huge arrays. I might be off here, but I think a double is 8 bytes long. That means you're trying to allocate 2 * 8 * 4 * 150,000 = 9,600,000 B = 9.6 MB of memory off the heap. Seeing as how you're program is assigned a fixed amount of memory upon execution (and it seems your program isn't assigned enough), you sometimes need to allocate memory on the heap if you are to operate on arrays that big. malloc() is your friend! :-) --> double *ncost = malloc(sizeof (double) * persons * scens); <-- This ought to do the trick. Just remember to make sure that malloc returns a valid pointer, otherwise you'll have another seg fault. I'm pretty sure you can adress the pointer like you do with the array there (ncost[persons][0], etc...); if not, you can always do ncost(sizeof(double) * persons + 0), etc... Hope this helps! -Henrik W Lund