www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Segmentation fault on de-referencing int field over 3 levels

reply Oliver Moeller <mol verify-it.de> writes:
Compiling the following with DMD and executing the program
yields an segmentation fault when accessing the public field 
'the_date.year' of another public field 'birth':


import std.c.stdio;
import std.c.stdlib;
import std.string;
import std.date;

public class MyDate {
  public:
  Date   the_date;       // only the field 'year' is relevant

  this(){ the_date.year = 1901; }
};

public class Author {
   public:
   MyDate   birth;
};

int main(char[][] args)
{
   Author author = new Author();
   MyDate mydate = new MyDate();

   printf("(1) accessing: mydate.the_date.year = %i\n", mydate.the_date.year);
   printf("(2) accessing: author.birth.the_date.year = \n");
   printf("%i\n",     author.birth.the_date.year);

   printf("Ok.\n");

   return 0;
}

To my understanding, this access is legal; if not, then the compiler should
refuse it.
Enlighten me if I am doing something wrong here (I am new to D).

Here is the screen output sniplet: -------------------------------------
phobos:~/Lang/D/Toy/segfault> make test
/home/oli/Projects/D/DMD/dmd/bin/dmd  -c -oftest_date.o test_date.d
/home/oli/Projects/D/DMD/dmd/bin/dmd -oftest_date.exe test_date.o
gcc test_date.o -o test_date.exe -m32 -lphobos -lpthread -lm

phobos:~/Lang/D/Toy/segfault> ./test_date.exe
(1) accessing: mydate.the_date.year = 1901
(2) accessing: author.birth.the_date.year =
Segmentation fault
-------------------------------------------------------------------------
Running test_date.d interpreted has the same effect (abort on (2)).

Versions: 
dmd: Digital Mars D Compiler v1.0
gcc: gcc (GCC) 4.0.2 20050901 (prerelease) (SUSE Linux)
platform: PC SuSE Linux 10.0 (64 bit)

** Computers will always make mistakes, but will do so much faster tomorrow.
** ------------------------------------------------- <omoeller verify-it.de>
Feb 23 2007
parent "Frank Benoit (keinfarbton)" <benoit tionex.removethispart.de> writes:
the Date is a struct and is of value type. (1) is OK so.
The MyDate is a class and is a reference type. This is why (2) fails.
You did not initialize Author.birth.

 public class Author {
    public:
    MyDate   birth;
this(){ birth=new MyDate(); }
 };
Oliver Moeller schrieb:
 Compiling the following with DMD and executing the program
 yields an segmentation fault when accessing the public field 
 'the_date.year' of another public field 'birth':
 
 
 import std.c.stdio;
 import std.c.stdlib;
 import std.string;
 import std.date;
 
 public class MyDate {
   public:
   Date   the_date;       // only the field 'year' is relevant
 
   this(){ the_date.year = 1901; }
 };
 
 public class Author {
    public:
    MyDate   birth;
 };
 
 int main(char[][] args)
 {
    Author author = new Author();
    MyDate mydate = new MyDate();
 
    printf("(1) accessing: mydate.the_date.year = %i\n", mydate.the_date.year);
    printf("(2) accessing: author.birth.the_date.year = \n");
    printf("%i\n",     author.birth.the_date.year);
 
    printf("Ok.\n");
 
    return 0;
 }
 
 To my understanding, this access is legal; if not, then the compiler should
 refuse it.
 Enlighten me if I am doing something wrong here (I am new to D).
 
 Here is the screen output sniplet: -------------------------------------
 phobos:~/Lang/D/Toy/segfault> make test
 /home/oli/Projects/D/DMD/dmd/bin/dmd  -c -oftest_date.o test_date.d
 /home/oli/Projects/D/DMD/dmd/bin/dmd -oftest_date.exe test_date.o
 gcc test_date.o -o test_date.exe -m32 -lphobos -lpthread -lm
 
 phobos:~/Lang/D/Toy/segfault> ./test_date.exe
 (1) accessing: mydate.the_date.year = 1901
 (2) accessing: author.birth.the_date.year =
 Segmentation fault
 -------------------------------------------------------------------------
 Running test_date.d interpreted has the same effect (abort on (2)).
 
 Versions: 
 dmd: Digital Mars D Compiler v1.0
 gcc: gcc (GCC) 4.0.2 20050901 (prerelease) (SUSE Linux)
 platform: PC SuSE Linux 10.0 (64 bit)
 
 ** Computers will always make mistakes, but will do so much faster tomorrow.
 ** ------------------------------------------------- <omoeller verify-it.de>
Feb 23 2007