SysTime in a Struct

Jonathan M Davis jmdavisProg at gmx.com
Thu Mar 1 14:17:05 PST 2012


On Thursday, March 01, 2012 15:15:00 albatroz wrote:
> Hi,
> 
> I have defined this struct
> struct preEv {
> string edate; //010112
> string etime; //00:00:00
> string etext; //
> SysTime esystime;
> this (this) {
> SysTime esystime = SysTime(DateTime(
> Clock.currTime.year,
> to!int(this.edate[2..4]),
> to!int(this.edate[0..2]),
> to!int(etime[0..2]),
> to!int(etime[3..5]),
> to!int(etime[6..8])));
> }
> }
> 
> If I write to the sctruct and then print it I'm able to see the
> SysTime variable with a value.
> writeln(preEv) //previousEvents("140212", "05:13:26", "9 140212
> 05:13:26 d", "2012-Feb-14 05:13:26")
> 
> but if trying to get the value from the SysTime variable I get a
> Segmentation fault. Trying to read any other variable inside this
> struct will not be a problem.
> 
> writeln (preEv.esystime.day) // will compile but segfaults
> 
> On DMD32 D Compiler v2.058
> 
> Any correct way to do this?

A default-initialized SysTime is useless. It hasn't been properly initialized. 
SysTime contains a time zone object, which is a class and must be initialized 
at runtime (so directly initializing it won't work), and that object is null 
in SysTime.init (and it can't be anything else, because you can't construct a 
class at compile and have it persist to runtime). You need to actually 
initialize a SysTime before using it. So, using the init value of struct which 
has a SysTime as a member probably isn't a great idea.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list