# Is this a known issue?

**URL:** <https://ziggit.dev/t/is-this-a-known-issue/7542>\
**Category:** Help\
**Tags:** language\
**Created:** [December 27, 2024, 7:14pm UTC](https://ziggit.dev/t/is-this-a-known-issue/7542 "2024-12-27T19:14:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [December 27, 2024, 7:14pm UTC](https://ziggit.dev/t/is-this-a-known-issue/7542/1 "2024-12-27T19:14:51Z")

</div>

During advent of code I wrote this function:

```zig
fn groupPebbles(groups: *std.ArrayList(PebbleGroup)) !void {
    std.mem.sortUnstable(PebbleGroup, groups.items, {}, PebbleGroup.lessThan);

    const e = groups.items[0];
    try groups.append(e);

    ...
}

```

This worked fine at the time of writing it. About a week later, however, I updated Zig and it started segfaulting:

```txt
Segmentation fault at address 0x7f8124999000
/home/markus/.local/share/zigup/0.14.0-dev.2568+42dac40b3/files/lib/std/array_list.zig:263:13: 0x103d6e9 in append (11)
            new_item_ptr.* = item;
            ^
/home/markus/Development/aoc2024/src/11/main.zig:73:22: 0x103d3bf in groupPebbles (11)
    try groups.append(e);
                     ^

```

I am pretty sure that this is not supposed to happen, and the workaround I found was to simply create a new pebble group manually from the old one (`.{ .a = a, .b = b, ... }`-style).

I mean it’s fairly obvious what is happening here: The compiler is optimizing `const e` away, which already existed only to avoid this issue before the compiler update. This leads to the code only accessing `e` (aka `items[0]` when the array may have been resized, which leads to a segfault.

Is this known behaveior?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [December 27, 2024, 7:40pm UTC](https://ziggit.dev/t/is-this-a-known-issue/7542/2 "2024-12-27T19:40:20Z")

</div>

There are actually two optimizations here, one is the one you describe, and the second one is the parameter reference optimization when calling the append function.

> [@markus](#):
>
> Is this known behavior?

The compiler team is aware that these cause problems, and they [plan to remove parameter reference optimization](https://github.com/ziglang/zig/issues/5973#issuecomment-2380332493) and only apply it when the compiler can prove that there are no side effects.

> [@markus](#):
>
> I am pretty sure that this is not supposed to happen, and the workaround I found was to simply create a new pebble group manually from the old one (`.{ .a = a, .b = b, ... }`-style).

Another workaround would be to use a var instead of a const, vars will always create a copy:

```zig
var e = groups.items[0];
_ = &e; // Hack to hide unused mutability warning

```

---

<div class="post-metadata">

**Author:** ![markus](https://ziggit.dev/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ziggit.dev/u/markus)\
**Post date:** [December 27, 2024, 7:43pm UTC](https://ziggit.dev/t/is-this-a-known-issue/7542/3 "2024-12-27T19:43:53Z")

</div>

Thank you for the info!

I actually find it very impressive that the compiler managed to find so many ways to optimize here, just a shame it wasnt the place to do so.

---

<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:** [December 27, 2024, 9:04pm UTC](https://ziggit.dev/t/is-this-a-known-issue/7542/4 "2024-12-27T21:04:30Z")

</div>

See [ATTACK of the KILLER FEATURES](https://www.youtube.com/watch?v=dEIsJPpCZYg) for an in-depth explanation.

---

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [December 27, 2024, 10:12pm UTC](https://ziggit.dev/t/is-this-a-known-issue/7542/5 "2024-12-27T22:12:12Z")

</div>

It’s very easy to optimize when it doesn’t have to be correct.
