# Only 4 bytes of a pointer retrieved in C

**URL:** <https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899>\
**Category:** General Usage\
**Tags:** embedding\
**Created:** [February 7, 2018, 4:27pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899 "2018-02-07T16:27:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nextr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nextr/32/3131_2.png) [@nextr](https://discourse.julialang.org/u/nextr)\
**Post date:** [February 7, 2018, 4:27pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/1 "2018-02-07T16:27:27Z")

</div>

Hello everybody,

I’m having a problem with a Julia function which returns a pointer to an object. The pointer is coded in 8 bytes. This function has a C interface to be used form a C program.  
When I use this function in C, only the half of the value (the 4 less signicant bytes) is retrived. So, I can’t reuse the pointed object… ☹  
For information, I am working on a 64-bit Linux

To be more explicit, here’s my Julia code

```julia
type MyType
  param1::Int
  param2::Int
  function MyType()
    new(1,2)
  end
end

Base.@ccallable function create_object()::Ptr{Void}
  newobject = MyType()
  objectpointer = pointer_from_objref(newobject)
  println("Simple object pointer :")
  print_pointer(objectpointer)
  return objectpointer
end

Base.@ccallable function print_pointer(inputpointer::Ptr{Void})::Cint
  println("pointer : ")
  println(inputpointer)
  return 0
end

```

Here’s my C code :

```julia
    jl_value_t* simple_ptr;
    simple_ptr = create_object();
    printf("simple object created\n");
    print_pointer(simple_ptr);

```

And here’s the output :

```julia
Simple object pointer :
pointer : 
Ptr{Void} @0x00007eff6e125c10
simple object created
pointer : 
Ptr{Void} @0x000000006e125c10

```

The generation of the shared library to be used in C is made by [static-julia](https://github.com/JuliaComputing/static-julia) which execute the following commands :

- Populate “.ji” local cache:  
$JULIA\_HOME/bin/julia -Cx86-64 -J$JULIA\_HOME/lib/julia/sys.so --compile=yes --depwarn=yes --startup-file=no -e

- Build object file “julia\_example.o”:  
$JULIA\_HOME/bin/julia -Cx86-64 -J$JULIA\_HOME/lib/julia/sys.so --compile=yes --depwarn=yes --startup-file=no --output-o julia\_example.o -e

- Build shared library “libjulia\_example.so”:  
gcc -m64 -shared -o libjulia\_example.so julia\_example.o -std=gnu99 -I$JULIA\_HOME/include/julia -DJULIA\_ENABLE\_THREADING=1 -fPIC -L$JULIA\_HOME/lib -Wl,–export-dynamic -Wl,-rpath,/lib -Wl,-rpath,$JULIA\_HOME/lib/julia -ljulia

- Build executable file “julia\_example”:  
gcc -m64 -o julia\_example $JULIA\_HOME/static-julia/example.c libjulia\_example.so -std=gnu99 -I/include/julia -DJULIA\_ENABLE\_THREADING=1 -fPIC -L/lib -Wl,–export-dynamic -Wl,-rpath,/lib -Wl,-rpath,/lib/julia -ljulia ‘-Wl,-rpath,$ORIGIN’

What am I doing wrong? Is it due to a wrong compilation? the C code, the Julia code?  
Thank you for your help !

Nextor

---

<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:** [February 7, 2018, 4:37pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/2 "2018-02-07T16:37:40Z")

</div>

> [@nextr](#):
>
> Base.@ccallable function create\_object()::Ptr{Void}  
> newobject = MyType()  
> objectpointer = pointer\_from\_objref(newobject)  
> println(“Simple object pointer :”)  
> print\_pointer(objectpointer)  
> return objectpointer  
> end

This is wrong, and the returned pointer can be garbage. It’s unrelated to your issue though.

> [@nextr](#):
>
> Here’s my C code :

Please post the **FULL** C code and fix all compiler warnings. You likely declare the functions in C incorrectly.

---

<div class="post-metadata">

**Author:** ![nextr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nextr/32/3131_2.png) [@nextr](https://discourse.julialang.org/u/nextr)\
**Post date:** [February 7, 2018, 6:00pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/3 "2018-02-07T18:00:52Z")

</div>

Hello yuyi !  
Thank you for your answer. While I was responding to you, I found that I didn’t declare explicitely the extern functions. Indeed, when the functions are explicitely declared , it works OK.  
Your answer helped me to solve my problem. Thank you very much.

Anyway, why do you say that the Julia function is wrong? Why do you say that the pointer can be garbage?  
Is there any way to control garbage collector in order to avoid the object from being unreferenced?

Now, my C code is

```julia

// Standard headers
#include <string.h>
#include <stdint.h>
#include <stdio.h>

// Julia headers (for initialization and gc commands)
// #include "uv.h"
#include "julia.h"

#ifdef JULIA_DEFINE_FAST_TLS // only available in Julia 0.7+
JULIA_DEFINE_FAST_TLS()
#endif

extern jl_value_t* create_object();
extern void print_pointer(jl_value_t*);
extern void print_object(jl_value_t*);

// main function (windows UTF16 -> UTF8 argument conversion code copied from julia's ui/repl.c)
int main(int argc, char *argv[])
{
	
    // initialization
    libsupport_init();
    // jl_options.compile_enabled = JL_OPTIONS_COMPILE_OFF;
    jl_options.image_file = "libjulia_example";
    julia_init(JL_IMAGE_JULIA_HOME);

    
    jl_value_t* simple_ptr;
    simple_ptr = create_object();
    printf("simple object created\n");
    print_pointer(simple_ptr);
    print_object(simple_ptr);

    // JL_GC_POP();

    // Cleanup and gracefully exit
    jl_atexit_hook(0);
    return 0;
}

```

With a new Julia function :

```julia
Base.@ccallable function print_object(inputpointer::Ptr{Void})::Cint
  println("object my_type : ")
  retrieved_object = unsafe_pointer_to_objref(inputpointer)
  println(retrieved_object.param1)
  println(retrieved_object.param2)
  return 0
end

```

Thank you

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [February 7, 2018, 6:30pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/4 "2018-02-07T18:30:20Z")

</div>

> [@nextr](#):
>
> Anyway, why do you say that the Julia function is wrong? Why do you say that the pointer can be garbage?
> 
> Is there any way to control garbage collector in order to avoid the object from being unreferenced?

Julia will be able to free the memory immediately, since it can see that the object (which “holds” the memory) is no longer referenced after the call to `pointer_from_objref`.  
You’d need some way of keeping a reference to that object in Julia.

I find it easier for Julia code to call into C code, so that the objects are still referenced in the Julia memory, rather than trying to return pointers to Julia objects to C code.

---

<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:** [February 8, 2018, 6:41pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/5 "2018-02-08T18:41:51Z")

</div>

> [@nextr](#):
>
> Anyway, why do you say that the Julia function is wrong? Why do you say that the pointer can be garbage?

Becuase the object can be free’d.

> [@nextr](#):
>
> Is there any way to control garbage collector in order to avoid the object from being unreferenced?

Not impossible but that’s not the the right approach. You should just return the object directly and use `Any` or appropriate `Ref` as the return/argument type instead. `cfunction` certainly support this so I assume `@ccallable` does too.

---

<div class="post-metadata">

**Author:** ![nextr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nextr/32/3131_2.png) [@nextr](https://discourse.julialang.org/u/nextr)\
**Post date:** [February 9, 2018, 10:35am UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/6 "2018-02-09T10:35:37Z")

</div>

Hi Yichao,  
I understand your point of view. However, the reason that I am trying to do that this way is that I am reusing an existing Julia project and it have a large amonut of complex types, so it would represent a huge amount of work to translate everything to C. Besides, the C-side doesn’t need to read or handle the returned objects, but only to keep them to pass them back again as parameters.  
Thus, I prefer to try with my approach.  
Now, my question is : How can I safe my Julia objects to avoid them to be collected ? You say this is not impossible. May you guide me to do that. I don’t find the right way to do that.  
Thank you in advance.

---

<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:** [February 9, 2018, 2:56pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/7 "2018-02-09T14:56:07Z")

</div>

> [@nextr](#):
>
> Thus, I prefer to try with my approach.

It’s unclear what you mean. The many different julia type makes absolutely no difference.

> [@nextr](#):
>
> How can I safe my Julia objects to avoid them to be collected ?

Do what I said.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [February 9, 2018, 2:59pm UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/8 "2018-02-09T14:59:40Z")

</div>

> [@nextr](#):
>
> Now, my question is : How can I safe my Julia objects to avoid them to be collected ?

Make sure they are still referenced on the Julia side (the Julia object, not a `Ptr` to the memory where one is/was (it can disappear when GC happens otherwise).

---

<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:** [February 11, 2018, 12:48am UTC](https://discourse.julialang.org/t/only-4-bytes-of-a-pointer-retrieved-in-c/8899/9 "2018-02-11T00:48:28Z")

</div>

No don’t do that.
