# Retrieve strides from \`jl\_array\_t\`

**URL:** https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440
**Category:** General Usage
**Created:** [April 30, 2017, 7:34am UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440 "2017-04-30T07:34:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![SylvainCorlay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaincorlay/32/923_2.png) [@SylvainCorlay](https://discourse.julialang.org/u/SylvainCorlay)
#### Post date: [April 30, 2017, 7:34am UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/1 "2017-04-30T07:34:12Z")

</div>

Is there a means to retrieve the strides of a julia array from the C API?

[https://docs.julialang.org/en/stable/stdlib/arrays/#Base.strides](https://docs.julialang.org/en/stable/stdlib/arrays/#Base.strides)

The only property accessors available in `julia.h` apparently are

```cpp
JL_DLLEXPORT void *jl_array_ptr(jl_array_t *a);
JL_DLLEXPORT void *jl_array_eltype(jl_value_t *a);
JL_DLLEXPORT int jl_array_rank(jl_value_t *a);
JL_DLLEXPORT size_t jl_array_size(jl_value_t *a, int d);

```

If I can avoid recomputing the strides on the C++ side, that would be fantastic.

---

<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: [April 30, 2017, 1:50pm UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/2 "2017-04-30T13:50:32Z")

</div>

`jl_array_t` are `Array` and are always column major dense array.

---

<div class="post-metadata">

### Author: ![SylvainCorlay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaincorlay/32/923_2.png) [@SylvainCorlay](https://discourse.julialang.org/u/SylvainCorlay)
#### Post date: [April 30, 2017, 7:46pm UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/3 "2017-04-30T19:46:53Z")

</div>

I know, in `xtensor-julia`, we do compute the column-major strides.

See e.g. [https://github.com/QuantStack/xtensor-julia/blob/master/deps/xtensor-julia/include/xtensor-julia/jltensor.hpp#L134](https://github.com/QuantStack/xtensor-julia/blob/master/deps/xtensor-julia/include/xtensor-julia/jltensor.hpp#L134)

but I guess that you already compute strides and store them in the memory representation of Julia arrays for efficiency.

It would be more efficient to me to be able to access them, just like we do in `xtensor-python`, but wrapping the strides from the buffer protocol.

See e.g. [https://github.com/QuantStack/xtensor-python/blob/master/include/xtensor-python/pyarray.hpp#L494](https://github.com/QuantStack/xtensor-python/blob/master/include/xtensor-python/pyarray.hpp#L494)

Is there a means to access the strides computed by Julia from the C API?

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [April 30, 2017, 8:18pm UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/4 "2017-04-30T20:18:21Z")

</div>

No, the strides aren’t pre-computed and cached. Only the dimension sizes themselves are stored. Here’s the array header definition: [https://github.com/JuliaLang/julia/blob/4a814806811d13b350a3f6c04cb839ad4c9236f1/src/julia.h#L143-L162](https://github.com/JuliaLang/julia/blob/4a814806811d13b350a3f6c04cb839ad4c9236f1/src/julia.h#L143-L162)

And here’s the implementation of converting an n-dimensional index to an offset: [https://github.com/JuliaLang/julia/blob/1093977c989a3f7bef4e791d58055c30ee2fee10/src/builtins.c#L906-L927](https://github.com/JuliaLang/julia/blob/1093977c989a3f7bef4e791d58055c30ee2fee10/src/builtins.c#L906-L927)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 30, 2017, 8:45pm UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/5 "2017-04-30T20:45:34Z")

</div>

> [@mbauman](#):
>
> Only the dimension sizes themselves are stored

Well, the dimensions _are_ essentially the strides, in units of `sizeof` the elements. Typically, in C, you would access a Julia column-major array `A[i1,i2,i3]` via

```julia
Adata[i1-1 + n1*(i2-1 + n2*(i3-1))]

```

where `Adata` is a pointer (of the appropriate type) to the underlying data and `n1` and `n2` are the sizes of the first two dimensions, and I’ve subtracted 1 to convert between 1-based and 0-based indexing. By parenthesizing it like this, no additional stride calculations are needed.

---

<div class="post-metadata">

### Author: ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)
#### Post date: [May 1, 2017, 7:32am UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/6 "2017-05-01T07:32:56Z")

</div>

It’s not really related to the strides, but note that `ArrayRef` in CxxWrap.jl makes working with native Julia arrays from C++ slightly easier:  
[https://github.com/JuliaInterop/CxxWrap.jl#reference-native-julia-arrays](https://github.com/JuliaInterop/CxxWrap.jl#reference-native-julia-arrays)

The current API is limited to what I needed, so it doesn’t implement multi-dimensional indexing and you will still need to use @stevengj 's formula.

---

<div class="post-metadata">

### Author: ![SylvainCorlay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaincorlay/32/923_2.png) [@SylvainCorlay](https://discourse.julialang.org/u/SylvainCorlay)
#### Post date: [May 1, 2017, 2:11pm UTC](https://discourse.julialang.org/t/retrieve-strides-from-jl-array-t/3440/7 "2017-05-01T14:11:08Z")

</div>

Thanks guys, this clarifies things quite a bit.
