Newbie Question - memory overlays
Fredrik Olsson
peylow at gmail.com
Tue Oct 24 00:56:34 PDT 2006
Jacques Collin skrev:
> My apologies if this has been answered before -- in which case,
> kindly point me to the source.
>
> Delphi (Borland) allows one to overlay memory variables
>
> " To create a new variable that resides at the same address as an
> existing variable, use the name of the existing variable (instead
> of an address) after the word absolute. For example,
>
> var
> Str: string[32];
> StrLen: Byte absolute Str;
>
> specifies that the variable StrLen should start at the same
> address as Str. "
>
> Question: Does D implement somrthing similar?
> This yould be useful if I could refer to a slice of a vector by a
> different name.
>
> --- thanks --
It seems you could fake the same functionality with something like this:
char[32] Str;
(inout char StrLen) {
// ... the rest of your function
} (Str[0]);
Some problems here though, if I try to use "inout byte StrLen" I get
"cast(byte)((Str)[0]) is not an lvalue". And is the inout argument
guaranteed to be "volatile" or will the compiler optimize read writes?
But as the dynamic arrays in D are way better then old school pascal
strings I see no need. As a matter of fact strings and dynamic arrays
have worked in Object Pascal (Delphi) in pretty much the same way as D
does it since v1.0 of Delphi.
Overall I think the idea is dangerous, it will work poorly endian-wise
for anything except byte size types.
// Fredrik Olsson
More information about the Digitalmars-d
mailing list