[OT] gcc: maximum length of an array?

Giorgos Keramidas keramida at ceid.upatras.gr
Sun Jul 23 18:05:32 UTC 2006


On 2006-07-24 20:49, "P.U.Kruppa" <root at pukruppa.de> wrote:
> Hi,
>
> sorry for posting an [OT], but usually people on this list know
> everything :-)
>
> Since I don't know too much about programming I am frequently
> fascinated by simple things like Eratosthenes' sieve.  As you might
> remember, one has to create a boolean array for that. The longer the
> array the more primes can be found.
>
> With malloc() I can create an array of length 100000000 (10^8) and the
> first 5761455 primes are calculated in a few seconds.  So of course I
> would like to test length 10^9 but here my program crashes.

If this is about integer values, which are probably 32-bit, you are
hitting the kern.maxdsiz limit of 512 MB.  An array of 100,000,000
32-bit values takes up 4 * 100,000,000 = 400,000,000 (close to 400 MiB
of memory to store).  Anything above 512 MB in size will make the data
size of your program so big that it will overflow the data seg size:

    $ ulimit -a
    core file size          (blocks, -c) unlimited
    data seg size           (kbytes, -d) 524288
    ...

You can either increase kern.maxdsiz in your `/boot/loader.conf' file,
or redesign the algorithm to work with larger datasets by splitting them
in chunks that you can still process with 512 MB of data :)



More information about the freebsd-questions mailing list