# Julia buffer interface?

**URL:** https://discourse.julialang.org/t/julia-buffer-interface/35922
**Category:** General Usage
**Tags:** arrays
**Created:** [March 13, 2020, 11:08am UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922 "2020-03-13T11:08:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [March 13, 2020, 11:08am UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/1 "2020-03-13T11:08:25Z")

</div>

Does Julia have anything akin to Python’s buffer interface? That is, I’d like to be able to test whether an object is represented as a contiguous/strided mutable/immutable buffer of bytes/words/etc in memory, and if so, extract its pointer and size. In my particular use-case, I’m only really interested in the basic case that an object is a contiguous buffer of bytes, e.g. `Array`, `SubArray`, `String`, `SubString`, …

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [March 13, 2020, 11:25am UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/2 "2020-03-13T11:25:38Z")

</div>

There isn’t really a concept of a pointer in regular julia semantics. Arrayelements aren’t necessarily stored in-line either.

Arrays themselves can act like a buffer with `push!` and `pop!` semantics (see [here](https://docs.julialang.org/en/v1/base/collections/#Dequeues-1)).

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [March 13, 2020, 11:44am UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/3 "2020-03-13T11:44:29Z")

</div>

I don’t mean an `IOBuffer`, I mean buffer in the sense of the [Python buffer protocol](https://docs.python.org/3/c-api/buffer.html) which is a way to query whether an object is represented as a bunch of regularly-spaced bytes in memory, and if so find out the address of that memory.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 13, 2020, 12:04pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/4 "2020-03-13T12:04:28Z")

</div>

The way to find out the address is basically to convert it to pointer for ccall.

I don’t think there’s a easy, builtin and generic way to find out if the conversioin is supported.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 13, 2020, 12:27pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/5 "2020-03-13T12:27:23Z")

</div>

I’m not aware of anything generic. I don’t know of a method that’s basically “Give me your data!” that you can pass an object to and expect a pointer to the underlying data back.

This all begs the question of “what” you are trying to do…do you really need a pointer to the memory? Why do you want a pointer to the memory? And so forth…

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [March 13, 2020, 12:35pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/6 "2020-03-13T12:35:17Z")

</div>

What I’m actually doing is passing this data into a C function, which wants a pointer and a length. For speed and memory reasons, I don’t want to make a copy of the data, so currently I restrict to `Array` and `String` inputs which I know how to extract the pointer and size from. I was hoping to abstract this away and not care too much what the data is, so long as it’s a bunch of bytes.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 13, 2020, 12:57pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/7 "2020-03-13T12:57:20Z")

</div>

Well you can probably get the pointer to ANYTHING…however in the case of strings and arrays that might not be the pointer you WANT. In the strings and array cases the pointer to the object points to the memory location where the size, length, and pointer to the real buffer resides. So yeah, there is no “give me a pointer to your real data” method you can call.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 13, 2020, 1:06pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/8 "2020-03-13T13:06:12Z")

</div>

> [@pixel27](#):
>
> So yeah, there is no “give me a pointer to your real data” method you can call.

As I said above, this part does exist. There just isn’t a way to determine if this is supported.

Also given,

> [@cjdoris](#):
>
> What I’m actually doing is passing this data into a C function

It seems that he already know about this part.

* * *

For this specific case, if you are just trying to avoid copying, I’d just ask for a conversion to pointer in `ccall` directly. The types can implement the proper method that does the copying.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [March 13, 2020, 1:17pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/9 "2020-03-13T13:17:44Z")

</div>

> [@yuyichao](#):
>
> It seems that he already know about this part.

Thanks 🙂

> [@yuyichao](#):
>
> For this specific case, if you are just trying to avoid copying, I’d just ask for a conversion to pointer in `ccall` directly. The types can implement the proper method that does the copying.

Is doing that definitely OK? For example, I wouldn’t want a strided array to be accepted.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 13, 2020, 4:16pm UTC](https://discourse.julialang.org/t/julia-buffer-interface/35922/10 "2020-03-13T16:16:08Z")

</div>

It seems that a strided array is allowed… which alsos makes some sense.

But that’s sth you can check with `strides`/`stride`.
