Hello there,

First time to experiment with DLang, after a long time.

I'm getting a weird behavior with an **array of chars**, where I get a segmentation fault upon writing to it (see code and output below). What makes this problem weird it two things:

1) Why there is no problem with the **int** array (unlike the **char** array)? 2) Why reading the array element (**charArray[index]**) works, unlike writing to it?

The problem is gone if I uncomment the constructor code.

Code:

     import std.stdio;

     void main()
     {
       (new Test).runit();
     }

     class Test {
       static enum MAX = 10;
       uint index = 0;
       auto intArray = new int[MAX];
       auto charArray = new char[MAX];

       this() {
         /*
         charArray = new char[MAX];
         */
       }

       void runit() {
         debug writefln("IntArray initially: %s", intArray);
         intArray[index] = 40; //OK
         debug writefln("IntArray becomes: %s", intArray);

debug writefln("Adding char in place of (%c) at index (%d)..",
                        charArray[index], index);
charArray[index] = 'a'; //ERROR: segmentation fault (code -11)! debug writefln("CharArray becomes %s (PROGRAM ABORTS BEFORE IT ACTUALLY!)",
                       charArray);
       }
     }


Output:

     $ dub run
Starting Performing "debug" build using /home/john/dlang/dmd-2.106.0/linux/bin64/dmd for x86_64. Building hello-dlang ~master: building configuration [application]
          Linking hello-dlang
          Running hello-dlang
     IntArray initially: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     IntArray becomes: [40, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     Adding char in place of (�) at index (0)..
     Error Program exited with code -11

Reply via email to