# UnsafePointers.jl: Convenient (but unsafe) pointer accesses

**URL:** https://discourse.julialang.org/t/unsafepointers-jl-convenient-but-unsafe-pointer-accesses/44750
**Category:** Package Announcements
**Created:** [August 11, 2020, 4:19pm UTC](https://discourse.julialang.org/t/unsafepointers-jl-convenient-but-unsafe-pointer-accesses/44750 "2020-08-11T16:19:33Z")
**Posts on this page:** 3
**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: [August 11, 2020, 4:19pm UTC](https://discourse.julialang.org/t/unsafepointers-jl-convenient-but-unsafe-pointer-accesses/44750/1 "2020-08-11T16:19:34Z")

</div>

Sometimes you need to interface with a C library. Sometimes that C library gives you pointers to structs containing pointers to arrays of pointers to…

```julia
# In C you do this:
p->second_field[3] = 9

# In Julia you do this:
unsafe_store!(unsafe_load(unsafe_load(p) + fieldoffset(eltype(p), 2)) + 3*sizeof(fieldtype(eltype(p), 2)), 9)

# Now you can do this:
q = UnsafePtr(p)
q.second_field[][4] = 9

```

This package exports one type, `UnsafePtr{T}`, which behaves similarly to a regular `Ptr{T}` but has some convenient (but unsafe) pointer access semantics.

Also `Array(q, dims...)`, `view(q, start:stop)`, `String(q)` are various ways to reinterpret array data.

Under the hood we are calling the `unsafe_*` family of functions. This library is a way of declaring once that you know what you are doing, and thereafter getting nice syntax for unsafe operations.

> **[GitHub - cjdoris/UnsafePointers.jl: Convenient (but unsafe) pointer access](https://github.com/cjdoris/UnsafePointers.jl)**
>
> Convenient (but unsafe) pointer access. Contribute to cjdoris/UnsafePointers.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [August 12, 2020, 8:26pm UTC](https://discourse.julialang.org/t/unsafepointers-jl-convenient-but-unsafe-pointer-accesses/44750/2 "2020-08-12T20:26:54Z")

</div>

Hm, I wonder I we can interface that with UnsafeArrays.jl …

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [August 12, 2020, 9:38pm UTC](https://discourse.julialang.org/t/unsafepointers-jl-convenient-but-unsafe-pointer-accesses/44750/3 "2020-08-12T21:38:02Z")

</div>

Also consider [GitHub - RelationalAI-oss/Blobs.jl: Binary blobs with on-the-fly pointer patching](https://github.com/RelationalAI-oss/Blobs.jl)
