# Feel confused with lifetime of anonymous struct

**URL:** <https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977>\
**Category:** Help\
**Tags:** language\
**Created:** [January 23, 2024, 7:17am UTC](https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977 "2024-01-23T07:17:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimying](https://ziggit.dev/user_avatar/ziggit.dev/jimying/32/1503_2.png) [@jimying](https://ziggit.dev/u/jimying)\
**Post date:** [January 23, 2024, 7:17am UTC](https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977/1 "2024-01-23T07:17:17Z")

</div>

In [general\_purpose\_allocator.zig](https://github.com/ziglang/zig/blob/68ea1121fc73d35311b29d15b731a3e424867265/lib/std/heap/general_purpose_allocator.zig#L302)

```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.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [January 23, 2024, 7:51am UTC](https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977/2 "2024-01-23T07:51:12Z")

</div>

The lifetime of [VTable](https://github.com/ziglang/zig/blob/68ea1121fc73d35311b29d15b731a3e424867265/lib/std/mem/Allocator.zig#L17) is [static](https://ziglang.org/documentation/master/#Static-Local-Variables).

---

<div class="post-metadata">

**Author:** ![tensorush](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@tensorush](https://ziggit.dev/u/tensorush)\
**Post date:** [January 23, 2024, 8:11am UTC](https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977/3 "2024-01-23T08:11:02Z")

</div>

Here’s a recent discussion on the same topic:

> [@Diving deep into anonymous struct literals](https://ziggit.dev/t/diving-deep-into-anonymous-struct-literals/2931):
>
> [Anonymous struct literals](https://ziglang.org/documentation/master/#Anonymous-Struct-Literals) are widely used throughout Zig stdlib, especially implementing vtable in all allocators. Documentation, unfortunately, has a lot to be desired and this thread carries over and expands the conversation that started in [another thread](https://ziggit.dev/t/how-to-downcast-an-allocator/2905) that became too long. Some fundamental questions first: Where do &.{...} live? On stack, in heap, in data or text segment? How long do they live? Local, static? If I instantiate the same anonymous struct literal twice, do I get two distinc…

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [January 23, 2024, 8:52am UTC](https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977/4 "2024-01-23T08:52:17Z")

</div>

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 🙂 )

---

<div class="post-metadata">

**Author:** ![slonik-az](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@slonik-az](https://ziggit.dev/u/slonik-az)\
**Post date:** [January 23, 2024, 10:56am UTC](https://ziggit.dev/t/feel-confused-with-lifetime-of-anonymous-struct/2977/5 "2024-01-23T10:56:59Z")

</div>

> [@AndrewCodeDev](#):
>
> (@slonik-az hopefully you don’t mind that I moved the comments over - just trying to keep things tidy 🙂 )

@AndrewCodeDev : Thanks for consolidating all discussions on this topic into a [single thread](https://ziggit.dev/t/diving-deep-into-anonymous-struct-literals/2931). 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.
