Moving structs containing strings ...

Bob W nospam at aol.com
Mon Apr 24 08:11:14 PDT 2006


"Derek Parnell" <derek at psych.ward> wrote in message 
news:op.s8if7ipr6b8z09 at ginger.vic.bigpond.net.au...
>
> .........
>
> However, there's a typo in it. Here is a fuller test of it ...
>
> .........

While I like your source in principal, I am
somewhat allergic to sequential memory
allocations. Therefore I have made some
modifications (see sample below).

std.file.read() is probably still not working
properly with large files due to a similar
reason.

Please note that this is my last post about
the subject and I definitely do not intend to
start anything like a programming competition
(I wish I had the time for it though).

My own program, which looks completely
different than the simplified examples, was
fixed in a couple of minutes after reading
kris' post. So I won't concentrate on this
issue any further.

------------------


import std.stdio;

alias std.stdio.writefln wrl;

struct Sc {
  uint val;  char[2] idz;  char[] id;
  void opCall() { id=idz; }
  void opCall(char[] z, uint v) { idz[]=z;  val=v;  id=idz; }
}

Sc[] arr;


void ShowMe(char[] msg) {
  wrl("\n%s ...", msg);
  foreach (uint i, Sc s; arr)
    wrl("arr[%d] - val:%d, id:'%s'", i,s.val,s.id);
}

void main() {
  // DmD documentation - quote: " ... Resizing a dynamic
  //   array is a relatively expensive operation ..."

  arr.length=99;  // leave enough space for ev. additions

  uint n=0;
  arr[n++]("ab",101);
  arr[n++]("cd",102);
  arr[n++]("ef",103);
  arr[n++]("gh",104);
  arr.length=n;   // now cut it's size back

  ShowMe("Original");

  // delete 1st item

  for (uint i=1; i<arr.length; i++) {
    arr[i-1]=arr[i];  arr[i-1]();
  }
  arr.length=arr.length-1;   // adjust length

  ShowMe("Updated");
}






More information about the Digitalmars-d-bugs mailing list