digitalmars.D.learn - malloc(s)[0..s] vs cast(T)malloc(s)
- Jack (11/14) Oct 14 2020 ?
- Paul Backus (10/24) Oct 14 2020 The difference is that the first version gives you a `void[]`,
- Jack (4/15) Oct 15 2020 My bad, the first one doesn't perform bounds-checking.So it just
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/8) Oct 14 2020 https://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocat...
- Jack (2/10) Oct 15 2020 make sense, thanks
What's the difference between:import core.stdc.stdlib : malloc; auto x = malloc(s)[0..s];andauto 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
Oct 14 2020
On Wednesday, 14 October 2020 at 20:15:39 UTC, Jack wrote:What's the difference between: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.htmlimport core.stdc.stdlib : malloc; auto x = malloc(s)[0..s];andauto 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
Oct 14 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: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()[...]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
Oct 15 2020
On 10/14/20 1:15 PM, Jack wrote:auto x = malloc(s)[0..s];https://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation Note that 'x' is passed to emplace() at that link and emplace() requires a slice. That's why the a slice is made from the pointer returned by malloc(). Ali
Oct 14 2020
On Thursday, 15 October 2020 at 01:22:54 UTC, Ali Çehreli wrote:On 10/14/20 1:15 PM, Jack wrote:make sense, thanksauto x = malloc(s)[0..s];https://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation Note that 'x' is passed to emplace() at that link and emplace() requires a slice. That's why the a slice is made from the pointer returned by malloc(). Ali
Oct 15 2020