Newbie Question - memory overlays
Sean Kelly
sean at f4.ca
Mon Oct 23 10:48:02 PDT 2006
Jacques Collin wrote:
> I union would not fit the job.
>
> In Delphi, I can have, for example,
> -- fdata1 : file of infonode
> -- datastore : array[1..n] of infonode;
> -- ctrlpoint : array[1..100] of infonode absolute datastore[1]
> where
> the first 100 "infonodes" can be used as an array. And the rest of
> the "datastore" can be used as a list or even a tree.
>
> at some point the "datstore" will be written out to disk.
> I would prefer to avoid a cumbersome work-around like a union
> (a.k.a variant records) in favour of the elegant Delphi solution
> of simply using the "absolute" attribute.
>
> Any suggestions?
I don't think D has support for this in the general sense, but you can
fake quite a lot with slices:
void main()
{
byte[8] buf;
char[] chars = cast(char[]) buf[4 .. $];
int[] ints = cast(int[]) buf[0 .. 4];
int* aint = cast(int*) &buf[0];
void print()
{
foreach( b; buf )
printf( "%d: %c\n", b, b );
printf( "\n" );
}
buf[] = cast(byte[8]) "abcdefgh";
print();
chars[] = "abcd";
print();
ints[0] = 0;
print();
*aint = uint.max;
print();
}
prints:
97: a
98: b
99: c
100: d
101: e
102: f
103: g
104: h
97: a
98: b
99: c
100: d
97: a
98: b
99: c
100: d
0:
0:
0:
0:
97: a
98: b
99: c
100: d
-1:
-1:
-1:
-1:
97: a
98: b
99: c
100: d
More information about the Digitalmars-d
mailing list