digitalmars.D - Newbie Question - memory overlays
- Jacques Collin (15/15) Oct 22 2006 My apologies if this has been answered before -- in which case,
- Chris Miller (4/19) Oct 22 2006 Try a union, http://www.digitalmars.com/d/struct.html
- Jacques Collin (14/14) Oct 23 2006 I union would not fit the job.
- Sean Kelly (57/73) Oct 23 2006 I don't think D has support for this in the general sense, but you can
- Fredrik Olsson (16/37) Oct 24 2006 It seems you could fake the same functionality with something like this:
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 --
Oct 22 2006
On Sun, 22 Oct 2006 21:54:51 -0400, Jacques Collin <zerozerosix hotmail.com> wrote: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 --Try a union, http://www.digitalmars.com/d/struct.html where each field starts at the union's start address.
Oct 22 2006
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? Thanks
Oct 23 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
Oct 23 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
Oct 24 2006