# Dereferencing Ptr{Ptr{Int32}}

**URL:** <https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709>\
**Category:** New to Julia\
**Tags:** question, c\
**Created:** [December 11, 2024, 3:04pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709 "2024-12-11T15:04:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 3:04pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/1 "2024-12-11T15:04:13Z")

</div>

Hi I’m using the  
Julia C interface to call a function from my dll  
Everything works fine

 ![jp1](https://global.discourse-cdn.com/julialang/original/3X/6/2/62f0411e4c2eb2fdcece0e729b988c835fcffa23.png)

Until I try to dereference it

```julia
first_row = unsafe_load(result_ptr)
firsr_num = unsafe_load(first_row)
#=
The matrix form is something like this 3x3
[[ 1 0 0] [0 0 0] [0 0 0] ] 
=#
-> 1

```

This is okay, but if I want to iterate over `result_ptr, ` the code will crash.  
I think it is something like accessing `NULL`, after the first time dereferencing `result_ptr` it goes out of scope, I guess. So iteration can not be continued.  
for example calling after the above example `first_row = unsafe_load(result_ptr+1)` will crash the program.  
My question is, how should I iterate over `result_ptr`?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 11, 2024, 4:06pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/2 "2024-12-11T16:06:01Z")

</div>

You probably should do `unsafe_load(result_ptr, 2)` rather than `unsafe_load(result_ptr + 1)`. Julia doesn’t do pointer arithmetic like C does.

---

<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:** [December 11, 2024, 4:53pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/3 "2024-12-11T16:53:41Z")

</div>

If this is an actual `Matrix` and not an array of pointers, you could also use `unsafe_wrap` on the pointer so you don’t have to deal with pointers in your julia code.

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 5:41pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/4 "2024-12-11T17:41:33Z")

</div>

Thanks, but it is not what I want at all  
this is what the dereferenced result must be:

```julia
2 0 0
0 0 2
1 0 0

```

but trying this

```julia
for i in 1:3
    int_ptr = unsafe_load(result_ptr,1)
    for j in 1:3
        value = unsafe_load(int_ptr,j)
        println(value)
    end
end

```

but the this is how it does it: `when i=1 , unsafe_load(int_ptr,3)` should be `int[0][2]` but it is `int[1][1]`.  
But ` when i=3, unsafe_load(int_ptr,1)` **it is correct**.!  
This shows that **it only works properly for the first element of each row.**

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 5:42pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/5 "2024-12-11T17:42:37Z")

</div>

> [@Sukera](#):
>
> Matrix

it is not

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 5:47pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/6 "2024-12-11T17:47:19Z")

</div>

this is what I got for the iteration:

```julia
2 0 2
0 0 462174787
1 0 453865397

```

**as you can see even the last indices are garbage values**

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 5:49pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/7 "2024-12-11T17:49:59Z")

</div>

I made a typo here: (it is i not 1)

```julia
for i in 1:3
    int_ptr = unsafe_load(result_ptr,i)
    for j in 1:3
        value = unsafe_load(int_ptr,j)
        println(value)
    end
end

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 11, 2024, 6:21pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/8 "2024-12-11T18:21:43Z")

</div>

> [@NimaPoshtiban](#):
>
> Thanks, but it is not what I want at all

That’s quite an odd reaction to a suggestion that lets you avoid a hard crash.

But only you know how your data looks, so we can’t help you with more than the obvious mistakes.

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 6:45pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/9 "2024-12-11T18:45:25Z")

</div>

the interesting behavior was that when you try to access **out of the bound element it crashes!** I have no idea why the returning values have garbage values.  
memory scan shows values are **true values**! it is only the Julia that is behaving strangely.

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 11, 2024, 6:50pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/10 "2024-12-11T18:50:52Z")

</div>

yeah Thanks for that, But that was not my Question,

---

<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:** [December 11, 2024, 7:45pm UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/11 "2024-12-11T19:45:47Z")

</div>

Apologies for missing that you have an array of pointers - could you share a complete example, so that we can see the code crash on our machines?

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 12, 2024, 7:51am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/12 "2024-12-12T07:51:43Z")

</div>

The crash problem was solved thanks to

**GunnarFarneback**

but My main question remains unsolved.  
**How to dereference a Ptr{Ptr{Int32}}?**  
How do you extract all elements from it?  
this is the DLL function signature:

```julia
int**Answer(int**);

```

how do you deal with that one?  
**I want to learn how to dereference nested pointers properly.**

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 12, 2024, 8:15am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/13 "2024-12-12T08:15:12Z")

</div>

Your latest code looks good to me. If I create some artificial data it gives the expected results.

```julia
julia> x1 = Int32[0, 1, 2]; x2 = Int32[2, 3, 3]; x3 = Int32[2, 1, 1];

julia> data = [pointer(x1), pointer(x2), pointer(x3)];

julia> result_ptr = pointer(data)
Ptr{Ptr{Int32}} @0x00007aa6ae7f6e30

julia> for i in 1:3
           int_ptr = unsafe_load(result_ptr,i)
           for j in 1:3
               value = unsafe_load(int_ptr,j)
               println(value)
           end
       end
0
1
2
2
3
3
2
1
1

```

Apparently the data you get from your dll is somehow different but I have no way of guessing in what way. Maybe you can share what you found in your memory scan?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [December 12, 2024, 8:26am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/14 "2024-12-12T08:26:43Z")

</div>

I tested the following on Linux.

Created `lib.c`:

```julia
#include <stdio.h>
#include <stdlib.h>

int** new_array() {
    int** array = malloc(sizeof(int*) * 4);
    for (int i = 0; i < 4; i++) {
        array[i] = malloc(sizeof(int) * 5);
        for (int j = 0; j < 5; j++) {
            array[i][j] = 4 * i + j;
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
    return array;
}

```

Compiled with `gcc -shared -o lib.so -fPIC lib.c`.

I tried two options:

```julia
function new_array_wrap()
    ptr = ccall((:new_array, "./lib.so"), Ptr{Ptr{Int32}}, ())
    ptr_arr = unsafe_wrap(Array, ptr, 4)
    return [unsafe_wrap(Array, p, 5) for p in ptr_arr]
end

function new_array_load()
    ptr = ccall((:new_array, "./lib.so"), Ptr{Ptr{Int32}}, ())
    array = Array{Int32}(undef, 4, 5)
    for i in 1:4
        ptr_arr = unsafe_load(ptr, i)
        for j in 1:5
            array[i, j] = unsafe_load(ptr_arr, j)
        end
    end
    return array
end

```

I am getting:

```julia-repl
julia> new_array_wrap()
0 1 2 3 4
4 5 6 7 8
8 9 10 11 12
12 13 14 15 16
4-element Vector{Vector{Int32}}:
 [0, 1, 2, 3, 4]
 [4, 5, 6, 7, 8]
 [8, 9, 10, 11, 12]
 [12, 13, 14, 15, 16]

julia> new_array_load()
0 1 2 3 4
4 5 6 7 8
8 9 10 11 12
12 13 14 15 16
4×5 Matrix{Int32}:
  0 1 2 3 4
  4 5 6 7 8
  8 9 10 11 12
 12 13 14 15 16

```

`unsafe_wrap` might not be suitable for you (check the documentation). Also, this example does not `free` the memory, which you should do.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 12, 2024, 9:33am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/16 "2024-12-12T09:33:08Z")

</div>

Sadly I can’t do anything with the DLL since I’m on Linux.

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 12, 2024, 9:46am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/17 "2024-12-12T09:46:48Z")

</div>

yes that worked on my machine too!  
But I’m also doing allocation which requires`int**`

```julia
int**Answer(int**);

```

maybe the problem is here:

```julia
a = fill(0,(3,3))
a[3,1]=1
array_of_int_pointers = Array{Ptr{Cint}}(undef, 3,3)

for i in 1:3, j in 1:3
    array_of_int_pointers[i, j] = Libc.malloc(Csize_t(sizeof(Cint)))
    unsafe_store!(array_of_int_pointers[i, j],a[i,j])
    
end

```

also here free is not needed, after the program has closed, memory will reclaim by Windows.

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 12, 2024, 9:57am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/18 "2024-12-12T09:57:11Z")

</div>

(post deleted by author)

---

<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:** [December 12, 2024, 10:24am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/19 "2024-12-12T10:24:01Z")

</div>

> [@NimaPoshtiban](#):
>
> can not share it because I was using IdaPro for that and it successfully loaded the value for the `Answer` function

Just for those who don’t know, IdaPro is a disassembling/binary inspection tool, commonly used for reverse engineering.

Personally, I’d be _extremely_ suspicious of some random DLL/.so that I’m asked to download to verify a problem. Can you not share a minimal C example that we can compile ourselves?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 13, 2024, 8:04am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/20 "2024-12-13T08:04:44Z")

</div>

I don’t have time to dig deeper into this, but if I did my approach would be to print out every relevant address and memory content from the C program immediately before the return from the function. Then I would compare those to the pointer addresses and dereferenced values in Julia to pinpoint exactly where things diverge. In my experience this usually resolves matters in one way or another, though not always pointing to the problem I thought I had.

---

<div class="post-metadata">

**Author:** ![NimaPoshtiban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nimaposhtiban/32/214104_2.png) [@NimaPoshtiban](https://discourse.julialang.org/u/NimaPoshtiban)\
**Post date:** [December 13, 2024, 8:27am UTC](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709/21 "2024-12-13T08:27:48Z")

</div>

> [@GunnarFarneback](#):
>
> ress and memory content from the C program immediately before the return from the function. Then I would compare those to the pointer addresses and dereferenced values in Julia to pinpoint exactly where things diverge. In my experience this usually resolves matters in one way or another, though not always pointing to the p

Thanks for your efforts. Pointers are valid. I wonder if this is a **Windows-specific problem.**  
because I only had this issue with Julia, tried that C code in C#, python and they were all fine. also, the compiler is **MSVC not gcc or Clang**.`cl.exe /LD source.c `.  
I was surprised to see many Linux users here, because of buggy Nvidia drivers they had to deal with.

[Next page](https://discourse.julialang.org/t/dereferencing-ptr-ptr-int32/123709.md?page=2)
