Handling memory

There is any function or option just like c/c++ pointer in julia ?

In the upcoming Julia 1.11 you will have the Memory type which allows safe access to essentially raw memory.

Prior to that you can still work with things like Libc.malloc, Ptr, convert(Ptr{Int}, pointer), unsafe_load, unsafe_store! etc. but this is no more safe than C/C++ and highly unidiomatic for Julia. It would be better to just use an Array. There is basically no overhead if you ensure type stability.

5 Likes

Hi,

@abraemer’s answer is correct. But if you are new to Julia, I would answer by asking the question: why do you want pointers?

The reason I ask is that they are used very little, and only to solve some very specific problems. For example: the only point in my code were I use one is to pass it to a function, so the function can allocate memory for the output, and whatever the function has computed is not lost, even if the function returns with an exception.

Julia allows you allocate and deallocate memory on the heap (cf. Array) and share it, without explicitly using pointers.

Maybe you could give an example of where you would think of using a pointer, so we can give you ideas on how to do without pointer, in a “julian” way?

:slight_smile:

5 Likes