ldc executable crashes with this code

Basile B. b2.temp at gmx.com
Thu Feb 3 01:54:40 UTC 2022


On Thursday, 3 February 2022 at 01:51:34 UTC, Basile B. wrote:
> On Thursday, 3 February 2022 at 01:39:33 UTC, forkit wrote:
>> On Wednesday, 2 February 2022 at 23:30:50 UTC, H. S. Teoh 
>> wrote:
>>> [...]
>>
>> that explains ldc perhaps (although i don't really get it. 
>> It's cast to mutable and being assigned to mutable.
>>
>> in any case... ldc doesn't like it, but dmd is fine with this 
>> ??
>
> your cast from immutable to mutable is an undefined behavior, 
> this may work or not.
>
>> Note that casting away a const qualifier and then mutating is 
>> undefined behavior, too, even when the referenced data is 
>> mutable. This is so that compilers and programmers can make 
>> assumptions based on const alone. For example, here it may be 
>> assumed that f does not alter x:
>
> (from https://dlang.org/spec/const3.html#removing_with_cast)

the D safe way :

```
void main() @safe
{
     char[] palindrome = "able was I ere I saw elba".dup;
     writeln(palindrome);
     writeln(palindrome.reverse);
}
```


More information about the Digitalmars-d-learn mailing list