segmentation fault-- is my array too long?

Henrik W Lund henrik.w.lund at broadpark.no
Thu May 6 14:53:01 PDT 2004


> ------------------------------------------------------------------------
> 
> Subject:
> segmentation fault-- is my array too long?
> From:
> "Caroline Korves" <ckorves at hotmail.com>
> Date:
> Thu, 06 May 2004 14:58:43 -0400
> To:
> freebsd-questions at freebsd.org
> 
> To:
> freebsd-questions at 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 at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at 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


More information about the freebsd-questions mailing list