Date: Thu, 06 May 2004 23:12:40 +0200 From: Henrik W Lund <henrik.w.lund@broadpark.no> To: freebsd-questions@freebsd.org Cc: ckorves@hotmail.com Subject: Re: segmentation fault-- is my array too long? Message-ID: <409AAA48.50409@broadpark.no> In-Reply-To: <20040506190033.A45C616A5CF@hub.freebsd.org> References: <20040506190033.A45C616A5CF@hub.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
> ------------------------------------------------------------------------ > > Subject: > segmentation fault-- is my array too long? > From: > "Caroline Korves" <ckorves@hotmail.com> > 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 <stdlib.h> > #include <stdio.h> > #include <math.h> > > #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<persons; 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... /* AMENDMENT!!! */ In my haste, I totally forgot my pointer dereferencing. The correct way to reference a pointer as a two dimensional array is, of course, thus: *(ncost + (sizeof(double) * persons) + 0)) = 0.00; Silly me. Anyway, this should work. /* END AMMENDMENT */ Hope this helps! -Henrik W Lund
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?409AAA48.50409>