Deleting Entries from an Array

Lars Ivar Igesund larsivar at igesund.net
Wed Jun 27 03:13:59 PDT 2007


Daniel Biehl wrote:

> Hello,
> 
> I play a little bit with D to learn something more about it. One
> interesting feature is splicing of arrays. I wrote a little function to
> delete some entries from an array of chars. I thought it was easy, but I
> only get a memory access error (Speicherzugriffsfehler in german) under
> Linux with DMD 1.015 and gdc 0.24 and on Windows XP only with gdc 0.23.
> Where is the Bug? Me or Phobos/DMD/GDC?
> 
> module tt;
> 
> import std.stdio;
> 
> char[] cut(inout char[] s, size_t index, size_t count) {
>     s[index..length - count] = s[index+count..length].dup;
>     s.length = s.length - count;
>     return s;
> }
> 
> void main()
> {
>     char[] s = "Hello World";
> 
>     writefln(s.length);
>     writefln(s.cut(1,1));
>     writefln(s.length);
> 
> }
> 
> Greets Daniel

At least literals are put in read only memory on Linux, so you will have to
dup s before cutting it.

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango



More information about the Digitalmars-d mailing list