Segfault when using SysTime
Jonathan M Davis
jmdavisProg at gmx.com
Fri Nov 4 01:47:10 PDT 2011
On Friday, November 04, 2011 18:53:28 knommad wrote:
> Hi,
>
> (I'm not sure whether I should be posting these here...apologies if this is
> inappropriate).
>
>
> Test case: (test.d)
> import std.datetime;
> import std.stdio;
>
>
> void main()
> {
> // SysTime is a struct that should be default initialised
> SysTime unInitialisedTime;
>
> // Will segfault on this next line...
> writeln("Time is: ", unInitialisedTime );
>
> writeln("ending now...");
>
> }
>
> Compiled with dmd 2.056: dmd -wi -oftest test.d.
>
> Result:
> Segmentation Fault.
>
> I did attempt to delve into the intricacies of the std.datetime library, but
> understanding the techniques used is currently beyond my skill level.
>
> Obviously, something is not being initialised properly?
Similar to how float.init is not a valid value (float.NAN), SysTime.init is not
a valid value. Aside from arguments over whether a struct's init value should
be a value intended to be used or not, SysTime has a member which is a class -
TimeZone - and since CTFE can't handle classes - let alone one which is
initialized at compile time and then used at runtime - SysTime.init _cannot_
have a valid TimeZone and therofere SysTime.init cannot be valid (unless we
were to introduce the extra overhead of constantly checking that the TimeZone
isn't null and setting it to LocalTime if it is, which is unacceptable IMHO).
And since, SysTime.init isn't valid, you _must_ initialize a SysTime if you
want to avoid segfaults.
If you want to know more about std.datetime, I suggest that you read this
article:
http://d-programming-language.org/intro-to-datetime.html
- Jonathan M Davis
More information about the Digitalmars-d
mailing list