www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is this invalid code

reply "Daniel Davidson" <nospam spam.com> writes:
The following crashes on writeln, but looks reasonable. Is some 
form of initializing ctor required for RateCurve?

import std.datetime;
import std.range;
import std.stdio;

struct DateRate {
   Date date;
   double value = 0.0;
}

struct RateCurve {
   private immutable(DateRate)[] _data;
}

struct CFS {
   double initialValue;
   RateCurve growth;
}

void main() {
   auto s = CFS(1.0);
   // auto s = CFS(1.0, RateCurve()); // crashes
   // auto s = CFS(1.0, RateCurve([])); // works
   writeln(s);
}

Thanks
Dan
Oct 31 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/31/2013 08:29 PM, Daniel Davidson wrote:
 The following crashes on writeln, but looks reasonable. Is some form of
 initializing ctor required for RateCurve?

 import std.datetime;
 import std.range;
 import std.stdio;

 struct DateRate {
    Date date;
    double value = 0.0;
 }

 struct RateCurve {
    private immutable(DateRate)[] _data;
 }

 struct CFS {
    double initialValue;
    RateCurve growth;
 }

 void main() {
    auto s = CFS(1.0);
    // auto s = CFS(1.0, RateCurve()); // crashes
    // auto s = CFS(1.0, RateCurve([])); // works
    writeln(s);
 }

 Thanks
 Dan
You are not going to like my answer but this may be the 16-byte struct bug. Add something to RateCurve and your code works fine... :-/ struct RateCurve { private immutable(DateRate)[] _data; ubyte b; // <-- ADDED } Ali
Oct 31 2013
parent "Daniel Davidson" <nospam spam.com> writes:
On Friday, 1 November 2013 at 04:26:25 UTC, Ali Çehreli wrote:
 You are not going to like my answer but this may be the 16-byte 
 struct bug. Add something to RateCurve and your code works 
 fine... :-/

 struct RateCurve {
   private immutable(DateRate)[] _data;
     ubyte b;  // <-- ADDED
 }
I appreciate and hate the answer :-). I've had a variant of this code working for a while. The previous version had DateRate[] instead of immutable(DateRate)[]. Since DateRate has no immutable aliasing I was trying to go for a more string-like immutable by design class. My hope is that is not the problem, or if it is it rears its head only in the context of initializers that leave that member empty. In this example if I provided `CFS(1.0, RateCurve([]));` as the initializer then it worked fine. What exactly is the 16-byte bug? I can't imagine 16-byte structs are generally a problem. Thanks, Dan
Nov 01 2013