# \`reinterpret\` to a single value from an array of a smaller data type

**URL:** https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932
**Category:** General Usage
**Created:** [March 23, 2018, 11:56pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932 "2018-03-23T23:56:38Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 26, 2018, 5:45am UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/21 "2018-03-26T05:45:23Z")

</div>

I did have a look at the source

Is there a way to setup two position pointers into an IOBuffer, ie for separate read/write locations without the seeks? mark could kind of work, or a switchmark would be good, eg (seek the current mark as new pos + store current pos as the new mark)

```julia
  function switchmark(io::T) where T<:IO ##Adapted from reset()
    ismarked(io) || (io.mark = 0); ##throw(ArgumentError("$(T) not marked"))
    m = io.mark
    p = io.ptr-1; ##offset of 1
    seek(io, m)
    io.mark = p # must be after seek, or seek may fail
    return m
    end

```

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 26, 2018, 1:30pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/22 "2018-03-26T13:30:53Z")

</div>

> [@jameson](#):
>
> There’s many methods already provided for safely reading/writing primitive bit elements from IO.

Yeah, but those don’t do me much good for random access (unless there is something I’m missing).

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [March 26, 2018, 3:47pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/23 "2018-03-26T15:47:47Z")

</div>

> [@ExpandingMan](#):
>
> random access

seek / skip / position?

> [@y4lu](#):
>
> two position pointers into an IOBuffer

I think only as `PipeBuffer` or `append=true` (one writer at the end position, and the reader independent)

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [March 26, 2018, 3:58pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/24 "2018-03-26T15:58:23Z")

</div>

> [@jameson](#):
>
> seek / skip / position?

I think the problem is that the need to always ultimately resort to `read` and thereby move the position in the IO buffer every time you read from it is just way more complicated than simply representing it as a reinterpreted array. Clearly I need to look into this more thoroughly though, I may have rejected that solution too quickly when I first got into this.

I definitely tended to have it in my mind that `read` actually deletes data from the buffer, but I suppose that is never true. I’m definitely too ignorant about this and need to benchmark a prototype that uses only `IO` and no unsafe methods.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [March 26, 2018, 5:55pm UTC](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932/25 "2018-03-26T17:55:29Z")

</div>

I think it would be nice to have a non-allocating, fast `read(io, Float64)` (e.g.) in Base.

I’m aware of [`read!(s::IO, x::Ref{T}) where {T}`](https://github.com/JuliaLang/julia/blob/bf45de8c337931d2ef87f45cf463e078edfe91ce/base/io.jl#L592), but the `Ref` is still a little cumbersome to work with, because at least right now (on master) creating the `Ref` allocates (which is why `read(io, Float64)` allocates). So in order to achieve zero allocation, you have to have a cache that contains the pre-allocated `Ref`.

In the past, I’ve used [https://github.com/BioJulia/BufferedStreams.jl](https://github.com/BioJulia/BufferedStreams.jl) to get a `read(io, ::Type{Float64})` that doesn’t allocate:

[https://github.com/BioJulia/BufferedStreams.jl/blob/d7806bd418cbfe7516e18583ec6dd1758cf35d40/src/bufferedinputstream.jl#L190-L204](https://github.com/BioJulia/BufferedStreams.jl/blob/d7806bd418cbfe7516e18583ec6dd1758cf35d40/src/bufferedinputstream.jl#L190-L204)

i.e. what `ScottPJones` was talking about. Maybe having this kind of thing in Base for certain frequently-used bitstypes would be good. Or maybe we just wait for compiler improvements that elide the `Ref` allocation.

[Previous page](https://discourse.julialang.org/t/reinterpret-to-a-single-value-from-an-array-of-a-smaller-data-type/9932.md?page=1)
