# Field Lifetime of Heap-Allocated Struct

**URL:** <https://ziggit.dev/t/field-lifetime-of-heap-allocated-struct/5913>\
**Category:** Help\
**Tags:** memory-management\
**Created:** [September 7, 2024, 8:57am UTC](https://ziggit.dev/t/field-lifetime-of-heap-allocated-struct/5913 "2024-09-07T08:57:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amesaine](https://ziggit.dev/user_avatar/ziggit.dev/amesaine/32/3233_2.png) [@amesaine](https://ziggit.dev/u/amesaine)\
**Post date:** [September 7, 2024, 8:57am UTC](https://ziggit.dev/t/field-lifetime-of-heap-allocated-struct/5913/1 "2024-09-07T08:57:03Z")

</div>

Is `&Node{ .atom = 0}` a dangling pointer here or does it share the lifetime of its parent `Node`?

```zig
fn exprBindingPower(allocator: std.mem.Allocator, ... ) *Node {
    var left_ptr = allocator.create(Node) catch unreachable;
    left_ptr.* = switch (lexer_ptr.next()) {
        .atom => |ch| Node{ .atom = ch },
        .op => |op| blk: {
            const bp = BindingPower.prefix(op);
            const right_ptr = exprBindingPower(...);

            break :blk Node{
                .cons = .{
                    .left = &Node{ .atom = 0 }, // dubious lifetime 
                    .op = op,
                    .right = right_ptr,
                },
            };
        },
        // ...
    };

    // ...

    return left_ptr;
}

```

---

<div class="post-metadata">

**Author:** ![amesaine](https://ziggit.dev/user_avatar/ziggit.dev/amesaine/32/3233_2.png) [@amesaine](https://ziggit.dev/u/amesaine)\
**Post date:** [September 7, 2024, 9:47am UTC](https://ziggit.dev/t/field-lifetime-of-heap-allocated-struct/5913/2 "2024-09-07T09:47:10Z")

</div>

Correct me if I’m wrong, `.left = Node{ .atom = 0 }` here has a static lifetime so problem solved.

> [@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…

Here’s the declaration.

```zig
const Node = union(enum) {
    atom: u8,
    cons: struct {
        op: u8,
        left: *const Node,
        right: *const Node,
    },
}

```

---

<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:** [September 7, 2024, 9:51am UTC](https://ziggit.dev/t/field-lifetime-of-heap-allocated-struct/5913/3 "2024-09-07T09:51:09Z")

</div>

You are right. It is a pointer to statically allocated memory.
