Crash in struct opAssign
Don
nospam at nospam.com
Thu Dec 2 12:28:53 PST 2010
Adam Burton wrote:
> 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.
My guess is that it's doing:
a.d[] = 1;
and since d.length==0, that's a no-op.
It shouldn't compile.
More information about the Digitalmars-d-learn
mailing list