Maximum array size

Dave Dave_member at pathlink.com
Wed Apr 26 06:33:38 PDT 2006


Ivan Kazmenko wrote:
> Hi again!
> 
> Dave:
>> Why the need for such a large static array? Can't a dynamic array be used in
>> your code instead?
> 
> I could use dynamic array, yes, but I don't see any benefit since its size is
> fixed. Please explain why it could be better.
> 

Instead of

int[25_000_000] myarray;

just do

int[] myarray = new int[25_000_000];

and that will take care of the immediate problem you describe and let 
you get on with solving the problem.

Example:

import std.stdio, std.string;

int main(char[][] args)
{
     bool[] flags = new bool[25_000_000];
     int  count;

     count = 0;
     flags[] = 1;
     for(int i = 2; i < flags.length; i++)
     {
         if(flags[i])
         {
             // remove all multiples of prime: i
             for(int j = i + i; j < flags.length; j += i) flags[j] = 0;
             count++;
         }
     }

     writefln("Count: ",count);

     return 0;
}

- Dave



More information about the Digitalmars-d mailing list