malloc(s)[0..s] vs cast(T)malloc(s)

Jack jckj33 at gmail.com
Wed Oct 14 20:15:39 UTC 2020


What's the difference between:

>import core.stdc.stdlib : malloc;
>auto x = malloc(s)[0..s];
and
>auto x = cast(T)malloc(s);
?

I have been using the last but I saw in some code examples, like 
this[1] the first being used. What's the difference? in the first 
one bounds checking is performed, giving an error right away, 
right? whereas the cast would just turn the null into the class 
reference and manual check need to be done later. Is that the 
reason?

[1]: 
https://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation


More information about the Digitalmars-d-learn mailing list