#Can I use std.heap.GeneralPurposeAllocator(.{}){} without assigning to var?

1 messages · Page 1 of 1 (latest)

cold quail
#

Just wondering. std.heap.GeneralPurposeAllocator(.{}){}is generally assigned to a var or actually two like this:

var gpa = std.heap.GeneralPurposeAllocator(.{}){}
const alloc = gpa.allocator();

So why is gpa.allocator() assigned to another const? I couldn't figure out if it's possible to join those together. A naive std.heap.GeneralPurposeAllocator(.{}){}.allocator()throws errors. So I'm up to learning something here 😉

rocky frigate
#

.allocator function is defined as fn(self: *Self) so you must assign std.heap.GeneralPurposeAllocator(.{}){} to some mutable value

cold quail
#

Ah yes, I fct that up

#

Can I use std.heap.GeneralPurposeAllocator(.{}){} without assigning to var?

cold quail
rocky frigate
cold quail
#

That explains it then 😉

misty hill
#

Well, the thing about the std.mem.Allocator interface (which is what is returned by gpa's allocator function) is that it basically boils down to two fields: ptr: *anyopaque, vtable: *const VTable and of course, vtable is a pointer to a struct that consists of three runtime function pointers named alloc, resize, and free.
The first parameter to each of the aforementioned function pointers are ptr: *anyopaque, and what they end up doing is casting that to a pointer to the implementation type, e.g. to *std.heap.GeneralPurposeAllocator(.{}). Then they do whatever they do to allocate, resize, or free, the specified memory.
So std.mem.Allocator is, in essence, just a very fancy fat pointer to the implementation - and pointers have to point somewhere, like a stack variable declared via var. Alternatively you could heap allocate it, and have it live on the heap, but that's a lot of overhead to just hide one extra variable.