# Why does a value have to be \`var\` to be \`deinit\`-able?

**URL:** <https://ziggit.dev/t/why-does-a-value-have-to-be-var-to-be-deinit-able/8120>\
**Category:** Help\
**Tags:** language, standard-library\
**Created:** [January 24, 2025, 2:17am UTC](https://ziggit.dev/t/why-does-a-value-have-to-be-var-to-be-deinit-able/8120 "2025-01-24T02:17:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scubbo](https://ziggit.dev/user_avatar/ziggit.dev/scubbo/32/4058_2.png) [@scubbo](https://ziggit.dev/u/scubbo)\
**Post date:** [January 24, 2025, 2:17am UTC](https://ziggit.dev/t/why-does-a-value-have-to-be-var-to-be-deinit-able/8120/1 "2025-01-24T02:17:37Z")

</div>

Consider the following code:

```zig
const std = @import("std");

fn createAHashMap(allocator: std.mem.Allocator) std.AutoHashMap(u8, u32) {
    var response_map = std.AutoHashMap(u8, u32).init(allocator);
    response_map.put('a', 1) catch unreachable;
    response_map.put('b', 2) catch unreachable;
    return response_map;
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const my_map = createAHashMap(allocator);
    defer my_map.deinit();
    std.debug.print("Value of 'a' is {}\n", .{my_map.get('a').?});
}

```

I declared `my_map` as `const` because, to my mind, in the context of `main` it _is_ `const` - it’s not being mutated, none of its keys or values change. However, attempting to run this code gives:

```zig
scratch.zig:32:17: error: expected type '*hash_map.HashMap(u8,u32,hash_map.AutoContext(u8),80)', found '*const hash_map.HashMap(u8,u32,hash_map.AutoContext(u8),80)'
    defer my_map.deinit();

```

I’m curious about this design decision. Is deinit-ing seen as a type of mutation? I guess that’s technically true, but it doesn’t _feel_ like the same thing as “_adding/removing a value to/from a map_”. Is this a philosophical choice, or is there a technical reason why this had to be true? Or, is there a category of bug that this is saving programmers from writing?

---

<div class="post-metadata">

**Author:** ![biom4st3r](https://ziggit.dev/user_avatar/ziggit.dev/biom4st3r/32/4342_2.png) [@biom4st3r](https://ziggit.dev/u/biom4st3r)\
**Post date:** [January 24, 2025, 2:30am UTC](https://ziggit.dev/t/why-does-a-value-have-to-be-var-to-be-deinit-able/8120/2 "2025-01-24T02:30:31Z")

</div>

```zig
        pub fn deinit(self: *Self) void {
            self.unmanaged.deinit(self.allocator);
            self.* = undefined;
        }

```

Looks like it’s purely to poison the memory in Debug and Safe modes

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [January 24, 2025, 2:54am UTC](https://ziggit.dev/t/why-does-a-value-have-to-be-var-to-be-deinit-able/8120/3 "2025-01-24T02:54:48Z")

</div>

because the deinit function takes a **non const** pointer which cannot be made from a const variable, it does this as @biom4st3r said to poison the memory in debug and safe to make use after frees easier to catch
