Feel confused with lifetime of anonymous struct

In general_purpose_allocator.zig

pub fn allocator(self: *Self) Allocator {
    return .{
        .ptr = self,
        .vtable = &.{
            .alloc = alloc,
            .resize = resize,
            .free = free,

vtable is a pointer, and assigned value the address of an anonymous struct,
I feel confused with the lifetime of the anonymous struct.

Hope someone can help explain it clearly. Thanks.

The lifetime of VTable is static.

1 Like

Here’s a recent discussion on the same topic:


Thank you, @tensorush,

For context, I just now merged more posts from a related thread to the one that @tensorush linked to. I had a very interesting discussion with @slonik-az on this topic that has been added to the bottom of that linked thread.

(@slonik-az hopefully you don’t mind that I moved the comments over - just trying to keep things tidy :slight_smile: )


@AndrewCodeDev : Thanks for consolidating all discussions on this topic into a single thread. Easier to read it all in one place rather than scattered around.

BTW, I think this question belongs to FAQ. Struct literals are quite unique feature of Zig not found in other languages and it is very easy to get them wrong. They are rather different beast than number or string literals and internalizing it takes some effort.

1 Like