Bizarre way to 'new' arrays

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Jun 16 12:56:24 PDT 2006


BCS wrote:
> Tom S wrote:
>> Sean Kelly wrote:
>>
>>> Sadly, this isn't legal:
>>>
>>>     int* i = new int(5);
>>>
>>> To allocate and initialize an integer.  AFAIK there's no way around 
>>> having the assignment as a separate statement following the allocation.
>>
>>
>> int* i = (new int)[0..1] = 5;
>>
>>
> 
> import std.stdio;
> 
> void main()
> {        // works for more than one value
>     const static int[] store = [0,1,2,3,4,5,6,7,8,9];
> 
>         // get a value
>     int* i = store[5..6].dup.ptr;
>     writef(*i,\n);
> 
>         // change it
>     *i = 4;
>     writef(*i,\n);
> 
>         // original is unchanged
>     i = store[5..6].dup.ptr;
>     writef(*i,\n);
> }

Now try replacing this one:

int* bytes = new int(1024*1024);	// 1 MB

Doesn't scale all that well, does it? :P



More information about the Digitalmars-d mailing list