## digitalmars.D.learn - Compile-Time Functions - Allocating Memory?

• Era Scarecrow (17/17) Jul 22 2008 I'm trying to compose as a test, where i fill a precompiled list of num...
Era Scarecrow <rtcvb32 yahoo.com> writes:
``` I'm trying to compose as a test, where i fill a precompiled list of numbers in
an array. What step am i missing to make this part work? (I i'm using gdc). it
keeps complaining of a non-constant expression. Is there any way to make this
work?

bool []primes = tp(1000);

bool [] tp( int max )
{
bool[] tmp = new bool[](max);

for ( int cnt = 2; cnt < max; cnt++ )
tmp[cnt] = isprime( cnt );

return tmp;
}

bool isprime( int x )
{
for ( int cnt = 2; cnt < x; cnt++ )
if ( x % cnt == 0 )
return false;

return true;
}

Era
```
Jul 22 2008
Ary Borenszweig <ary esperanto.org.ar> writes:
```Era Scarecrow a écrit :
I'm trying to compose as a test, where i fill a precompiled list of numbers
in an array. What step am i missing to make this part work? (I i'm using gdc).
it keeps complaining of a non-constant expression. Is there any way to make
this work?

bool []primes = tp(1000);

bool [] tp( int max )
{
bool[] tmp = new bool[](max);

for ( int cnt = 2; cnt < max; cnt++ )
tmp[cnt] = isprime( cnt );

return tmp;
}

bool isprime( int x )
{
for ( int cnt = 2; cnt < x; cnt++ )
if ( x % cnt == 0 )
return false;

return true;
}

Era

lol,

Some days ago I was writing the same thing to make a video showing
Descent hovering over that constant and the primes would appear.

It seems this can't be interpreted:

new bool[](max);

You can't create anything on the heap when doing CTFE (afaik). I did it
by using a dynamic array:

bool[] tmp;
tmp ~= false;
tmp ~= false;
for ( int cnt = 2; cnt < max; cnt++ )
tmp ~= isprime( cnt );

And since CTFE seems to not have garbage collection, you might want to
do "cnt * cnt <= x" instead of "cnt < x" ;-)
```
Jul 22 2008
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
```"Era Scarecrow" <rtcvb32 yahoo.com> wrote in message
news:g664l0\$2bia\$1 digitalmars.com...
I'm trying to compose as a test, where i fill a precompiled list of
numbers in an array. What step am i missing to make this part work? (I i'm
using gdc). it keeps complaining of a non-constant expression. Is there
any way to make this work?

bool []primes = tp(1000);

Completely off-topic, but a stylistic point.  Putting the brackets next to
the name is a bit misleading in D, since unlike in C, type decorators do not
attach to the variables but instead to the base type.  That is,

char * x, y;

in C means that x is a pointer to a char, while y is a char; but in D,
they're both char*.  For that reason it's recommended that the
brackets/asterisks attach to the type.

(I still can't quite figure out why you also use "bool [] tp" and "bool[]
tmp".  Usually people are consistent about these things ;) )
```
Jul 22 2008