Memory safe in D

ShowMeTheWay ShowMeTheWay at gmail.com
Wed Apr 17 04:50:23 UTC 2024


On Wednesday, 17 April 2024 at 00:25:07 UTC, Basile B. wrote:
> On Tuesday, 16 April 2024 at 22:15:42 UTC, ShowMeTheWay wrote:
>>
>> btw. This too is a likely bug:
>>
>> int b;
>> writeln(b);
>>
>> The compiler should require you to assign to 'b' before using 
>> it.
>>
>> On the otherhand, this below should *not* get the compilers 
>> attention:
>>
>> int b = int.init;
>> writeln(b);
>
> Both are semantically equivalent. The first version is about 
> knowing how the language works, the second is about being 
> stupid. D policy about default initializers is really to create 
> clear poison value. You still have "void initialization" if you 
> want to introduce UBs.
>
> ```d
> int b = void;
> writeln(b);
> ```
>
> that is more what should get the compiler attention.

I don't agree.

Once it's been definitely assigned, the compiler should leave it 
to the programmer.

If there's a bug, then that is the programmers problem to deal 
with.

On the otherhand, use of a variable that has not yet been 
assigned to (regardless of its default value)... well that is 
almost certainly a bug, and a worthy target for the compilers 
attention.

Again, C# and Java compilers already do this.

I don't expect to see in the D compiler, given the priorities of 
D, but it would be nice to have.



More information about the Digitalmars-d mailing list