Bizarre way to 'new' arrays
BCS
BCS at pathlink.com
Fri Jun 16 15:15:04 PDT 2006
Frits van Bommel wrote:
> BCS wrote:
>>
[...]
>>
>
>
> Now try replacing this one:
>
> int* bytes = new int(1024*1024); // 1 MB
>
> Doesn't scale all that well, does it? :P
If you want an int[2^20] this wont do it. But if you want a dynamically
created int that holds that value, that can be done.
For a huge array, I'd ship it mapped into a data file, or something like
that.
The array in the first case is there for if you have more than one value.
e.i.: const static int[] store = [-3,5,7,26,1024,1022];
<code>
import std.stdio;
void main()
{ // works for more than one value
const static int[] store = [1024*1024];
// get a value
int* i = store[0..1].dup.ptr;
writef(*i,\n);
// change it
*i = 4;
writef(*i,\n);
// original is unchanged
i = store[0..1].dup.ptr;
writef(*i,\n);
int j;
j= 1024*1024;
// non const values anyone??
i = (&j)[0..1].dup.ptr;
// change it (again)
*i = 4;
writef(*i,\n);
// original is unchanged
writef(j,\n);
}
</code>
More information about the Digitalmars-d
mailing list