Crash in struct opAssign

Adam Burton adz21c at gmail.com
Thu Dec 2 09:32:41 PST 2010


olivier wrote:

> Hi,
> 
> This program, compiled with dmd 1.065 under Linux, crashes:
> 
> void main() {
> Num a = 1;
> }
> 
> struct Num {
> int[] d;
> 
> Num opAssign( int v ) {
> d.length = 1;
> d[0] = v;
> return *this;
> }
> }
> 
> It looks like d is not initialized before opAssign() is called.
> It doesn't crash if I replace "Num a = 1" with:
> Num a;
> a = 1;
> 
> Is this normal behaviour?
I don't believe the failing code uses opAssign. I believe that is 
initialization. In D2 "Num a = 1;" would be initialization or call a 
constructor if appropriate and "Num a; a = 1;" would use opAssign. Based on 
http://www.digitalmars.com/d/1.0/struct.html I am thinking it does the same 
(minus the constructors). So I think this might be a bug with struct 
initialization in D1, I don't think you should be able to just assign 1 to 
it.


More information about the Digitalmars-d-learn mailing list