# Int -\> Str -\> print(io,\_) without allocations

**URL:** https://discourse.julialang.org/t/int-str-print-io-without-allocations/134626
**Category:** General Usage
**Tags:** strings, memory-allocation, io
**Created:** [December 18, 2025, 8:49am UTC](https://discourse.julialang.org/t/int-str-print-io-without-allocations/134626 "2025-12-18T08:49:13Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [December 18, 2025, 1:17pm UTC](https://discourse.julialang.org/t/int-str-print-io-without-allocations/134626/7 "2025-12-18T13:17:49Z")

</div>

You can’t write directly to the buffer of an IOBuffer, because the docs of IOBuffer states:

> Passing `data` as scratch space to `IOBuffer` with `write=true` may give unexpected behavior  
> Once write is called on an IOBuffer, it is best to consider any previous references to data invalidated; in effect IOBuffer “owns” this data until a call to take!. Any indirect mutations to data  
> could lead to undefined behavior by breaking the abstractions expected by IOBuffer.

You have two options:

- Carry around your own buffer, e.g. a `Vector{UInt8}`, and then write your integers to that. Then flush that buffer to your IO when it’s reached a certain size. However, this is pretty annoying. What if you are writing a bunch of different stuff, interleaved, and just not integers? Why should you implement the flushing logic yourself? Etc. etc.
- Just use `BufferIO.jl`. For example:

```julia-auto
using BufferIO
# use arbitrary IO sink, could be a file or whatever, here IOBuffer
io = BufWriter(IOBuffer())
@allocated write(io, 5) # returns zero

```

---

_[View the full topic](https://discourse.julialang.org/t/int-str-print-io-without-allocations/134626)._
