# Reinterpret returns wrong values

**URL:** <https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759>\
**Category:** New to Julia\
**Created:** [July 19, 2023, 12:39am UTC](https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759 "2023-07-19T00:39:41Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 20, 2023, 7:21pm UTC](https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759/21 "2023-07-20T19:21:09Z")

</div>

You can also try a generated function like this to avoid the `Vector{Any}` of fields. Might be faster:

```julia
@generated function read_seq_generated(io, ::Type{T}) where T
    types = fieldtypes(T)
    quote
        Base.Cartesian.@ncall $(length(types)) $T i -> read(io, $types[i])
    end
end

```

In terms of performance I get:

```julia
@benchmark read_seq_generated(x, Adummy) setup=(x = IOBuffer(src)) evals=1

BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 42.000 ns … 2.666 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 125.000 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 118.733 ns ± 33.852 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

                                         █                      
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂ ▂
  42 ns Histogram: frequency by time 167 ns <

 Memory estimate: 80 bytes, allocs estimate: 3

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 20, 2023, 7:55pm UTC](https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759/22 "2023-07-20T19:55:03Z")

</div>

> [@Kadii](#):
>
> it’s unsafe since it bypasses the constructor

`read(stream, T)` isn’t entirely safe either, you rely on the stream’s bytes being neatly arranged in `read`-supported Julia types. For example, all primitive types are multiple of bytes, but there’s no guarantee that a data stream will adhere to that, maybe each datum only needs 1.5 bytes and you’re expected to bitmask and portably match the platform’s endianness.

> [@jules](#):
>
> You can also try a generated function like this
> 
> > [@jules](#):
> >
> > `Base.Cartesian.@ncall $(length(types)) $T i -> read(io, $types[i])`

A brief explainer, what a generated function can do that a normal generic function can’t is create a function body expression based on the argument types at the call. So `read_pad_interpret` has the same source code no matter the `T`, but `read_seq_generated` will look at `Adummy` and use the `@ncall` macro to write the equivalent of `Adummy(read(io, types[1]), read(io, types[2]), read(io, types[3]) ...` etc which is close to how you might manually write the most specific efficient call for `Adummy`. I want to hazard a guess that the indexing is done at compile-time because `fieldtypes` is a tuple, so it should be like if you wrote in the field types by hand too, but if it’s not I think you could write `:($T( $(( :(read(io, $ft)) for ft in fieldtypes(T) )...) ))`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 20, 2023, 8:14pm UTC](https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759/23 "2023-07-20T20:14:22Z")

</div>

Have you tried just using [StructIO.jl](https://github.com/JuliaIO/StructIO.jl)? i.e.

```julia
using StructIO

@io struct Adummy
    p1::SVector{4, UInt8}
    p2::SVector{2, UInt8}
    p3::UInt16
    p4::UInt32
    p5::UInt32
    p6::UInt32
    p7::UInt32
    p8::UInt8
    p9::UInt32
    p10::UInt32
end align_packed

unpack(io, Adummy) # reads Adummy struct from stream io, with no padding between fields

```

---

<div class="post-metadata">

**Author:** ![Kadii](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kadii](https://discourse.julialang.org/u/Kadii)\
**Post date:** [May 2, 2024, 7:33pm UTC](https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759/24 "2024-05-02T19:33:13Z")

</div>

[StructIO.jl](https://github.com/JuliaIO/StructIO.jl) just worked great. Thank you. Even I didn’t have to use `StaticArrays`.

```julia
@io struct Adummy
    p1::NTuple{4, UInt8}
    p2::NTuple{2, UInt8}
    p3::UInt16
    p4::UInt32
    p5::UInt32
    p6::UInt32
    p7::UInt32
    p8::UInt8
    p9::UInt32
    p10::UInt32
end align_packed

```

[Previous page](https://discourse.julialang.org/t/reinterpret-returns-wrong-values/101759.md?page=1)
