There’s a brand new package that seems like made for you. It’s probably not yet registered (you can still use/try it), nor announced:
In theory Julia could heap-allocate for you and free before exit of barw_array, but it would be slower. Or allocate on the stack, but it’s limited so dangerous, you don’t know how large the array is going to be, so I doubt that’s a better policy (or check at runtime the size and only stack-allocate if small).