www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Newbie Question - memory overlays

reply Jacques Collin <zerozerosix hotmail.com> writes:
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
next sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
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
parent reply Jacques Collin <zerozerosix hotmail.com> writes:
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
parent Sean Kelly <sean f4.ca> writes:
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
prev sibling parent Fredrik Olsson <peylow gmail.com> writes:
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 --

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