# Dealing with "C style" memory layout

**URL:** <https://discourse.julialang.org/t/dealing-with-c-style-memory-layout/68627>\
**Category:** General Usage\
**Tags:** question, c\
**Created:** [September 23, 2021, 12:06pm UTC](https://discourse.julialang.org/t/dealing-with-c-style-memory-layout/68627 "2021-09-23T12:06:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 23, 2021, 12:06pm UTC](https://discourse.julialang.org/t/dealing-with-c-style-memory-layout/68627/1 "2021-09-23T12:06:52Z")

</div>

In julia arrays are stored using the [fortran memory ordering](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-column-major). When interacting with other languages that use “C” ordering it is often necessary to convert between the two.  
Usually I do this in an add hoc manner like

```julia
permutedims(reshape(arr, reverse(size(arr))), (4,3,2,1))

```

but that is hard to read, error prone and maybe not optimally fast.

- How do other people deal with this? I am mostly interested in `ndims(arr) > 2`.
- Is there a dedicated package for doing such conversions?
- Is there maybe a dedicated `CArray <: AbstractArray` somewhere that is backed by “C-ordering”?
- Is there a more performant way than the above hack?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 23, 2021, 12:27pm UTC](https://discourse.julialang.org/t/dealing-with-c-style-memory-layout/68627/2 "2021-09-23T12:27:40Z")

</div>

you can just do `permutedims(arr, (4,3,2,1))`, no?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 23, 2021, 1:11pm UTC](https://discourse.julialang.org/t/dealing-with-c-style-memory-layout/68627/3 "2021-09-23T13:11:44Z")

</div>

That does look plausible, I think it might be that I am just confused 😅
