union/toString: crash/segfault: What's happening here?

ag0aep6g anonymous at example.com
Fri Jan 12 02:13:08 UTC 2018


On 01/12/2018 02:45 AM, Nicholas Wilson wrote:
> 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.

A null string is a perfectly fine empty string. Printing it does not 
lead to a segfault. And the thrown error is not a segfault.

Actually, the problem seems to be that s is not default initialized. 
It's garbage. And garbage usually isn't a proper string, so writeln 
fails. Looks like a compiler bug.


More information about the Digitalmars-d-learn mailing list