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

Jack jckj33 at gmail.com
Thu Oct 15 14:37:05 UTC 2020


On Wednesday, 14 October 2020 at 21:12:13 UTC, Paul Backus wrote:
> On Wednesday, 14 October 2020 at 20:15:39 UTC, Jack wrote:
>> [...]
>
> The difference is that the first version gives you a `void[]`, 
> and the second version gives you a `T`. Neither version does 
> any bounds checking.
>
> Generally, you'd use the first version if you don't yet know 
> what kind of object is going to be stored in the allocated 
> memory (for example, if you're writing an allocator[1]), and 
> the second version if you do know the type.
>
> [1] 
> https://dlang.org/phobos/std_experimental_allocator_building_blocks.html

My bad, the first one doesn't perform bounds-checking.So it just 
depends on context, where you are going to use the result from 
malloc()


More information about the Digitalmars-d-learn mailing list