union/toString: crash/segfault: What's happening here?
    Nicholas Wilson 
    iamthewilsonator at hotmail.com
       
    Fri Jan 12 01:45:37 UTC 2018
    
    
  
On Friday, 12 January 2018 at 00:54:03 UTC, kdevel wrote:
> crash.d
> ```
> import std.stdio;
>
> union U {
>    float f;
>    int i;
>    string toString ()
>    {
>       string s;
>       return s;
>    }
> }
>
> void main ()
> {
>    U u;
>    writeln (u);
> }
> ```
>
> $ dmd crash.d
> $ ./crash
>
because you don't initialise `s` in
>    string toString ()
>    {
>       string s;
>       return s;
>    }
so it defaults to `string s = null;` thus giving a segfault.
try `string s = "";` instead.
    
    
More information about the Digitalmars-d-learn
mailing list