www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Deleting Entries from an Array

reply Daniel Biehl <dbiehl gmx.net> writes:
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
Jun 27 2007
next sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
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
Jun 27 2007
next sibling parent reply Daniel Biehl <dbiehl gmx.net> writes:
Ah thats it!!

Thanks, but: 

Why is it work with DMD 1.016 on Windows XP (not with GDC!!!), there comes no
error. I know on Windows literals are not stored in read only memory, like on
Linux, but why I can write on Windows with DMD
char[] s = "Hello" and must write on Linux char[] s = "Hello".dup. Isn't it a
little inconsitent? Maybe the compiler should make a .dup if a initialize a
dynamic array variable?

Greets Daniel

Lars Ivar Igesund Wrote:

 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

Jun 27 2007
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Daniel Biehl wrote:
 Why is it work with DMD 1.016 on Windows XP (not with GDC!!!), there comes no
 error. I know on Windows literals are not stored in read only memory, like on
 Linux, but why I can write on Windows with DMD char[] s = "Hello" and must
 write on Linux char[] s = "Hello".dup. Isn't it a little inconsitent? Maybe
 the compiler should make a .dup if a initialize a dynamic array variable?
 

There's no reason not to allow char[] s = "Hello", you just can't write to it. The const semantics in D 2.0 would catch this error.
Jun 27 2007
prev sibling parent Daniel Biehl <dbiehl gmx.net> writes:
Lars Ivar Igesund Wrote:

 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

Jun 27 2007
prev sibling parent Daniel Biehl <dbiehl gmx.net> writes:
Daniel Biehl schrieb:
 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

gdb reports: Program received signal SIGSEGV, Segmentation fault. [Switching to Thread -1209001280 (LWP 10327)] 0x44e6bef5 in memcpy () from /lib/libc.so.6 Daniel
Jun 27 2007