4 ms·
I think the idea is that heap allocations are costly, so making them explicit exposes their cost clearly, and promotes careful thought about strategies for heap
by llimllib 5y ago
I think the idea is that heap allocations are costly, so making them explicit exposes their cost clearly, and promotes careful thought about strategies for heap allocaiton.
- ModernMech 5y agoHow is that any more explicit or intentional than calling malloc though? You have to tell it exactly how many bytes you want on the heap.
- anonymoushn 5y agoIt's more explicit to pass an allocator to things that will allocate than not to, in the same way that other local variables are more explicit than other global variables. By cultural convention, you'll be able to bring your own allocators to almost any library, so you won't have to let your libraries decide when your program performs heap allocations.
- formerly_proven 5y agoZig makes it supremely easy to use different allocators for different pieces so you can do much better than just calling malloc everywhere. This enables very easy and straightforward use of arena allocators in particular.
- sirwhinesalot 5y agoIf I call some function foo() I have no idea if it does any heap allocation internally. In zig I always know because all functions that allocate memory explicitly request an allocator. That also allows me to control which allocator they use.
- ModernMech 5y agoI see, I didn’t understand that’s what they meant about zig.
- flohofwoe 5y agoFor many types of programs it's really, really important to have control over allocation behaviour which malloc doesn't provide (for instance a per-frame bump allocator, an allocator which reuses memory blocks but still works within a pre-allocated memory chunk, a specialized allocator for GPU memory blocks, etc...).