# Create a type with pointer to array

**URL:** <https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 5, 2017, 4:29pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306 "2017-01-05T16:29:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzilhao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzilhao/32/9473_2.png) [@mzilhao](https://discourse.julialang.org/u/mzilhao)\
**Post date:** [January 5, 2017, 4:29pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/1 "2017-01-05T16:29:32Z")

</div>

hi all,

I’m new to Julia, and still getting used to the Julia-way of doing things. I was wondering if there is anything that would work as a pointer would in C, or as a reference variable in C++?

the particular use case I have in mind is as follows. suppose I create a new type, called “Coord”, that will store a particular coordinate system.

```julia
immutable Coord
    val::Array{Real}
    name::AbstractString
end

```

and use it to create my coordinate x:

```julia
x0 = 0:0.05:1
x = Coord(x0, "mycoord")

```

Now I want another type, called “MyField”, which I’d like to contain a field to point to a type Coord. Something like

```julia
type MyField
    coord::Coord
    val::Array{Real}
    name::AbstractString
end

```

Say that I now create two such objects:

```julia
A = Field(x, x0, "A")
B = Field(x, x0, "B")

```

My understanding is that both A and B now contain two copies of my Coord type, right? ie, A.coord and B.coord are independent and are each occupying space in memory. This is fine for small arrays, but for larger ones memory consumption will quickly increase needlessly, since I’d be fine if A.coord and B.coord would be pointing to the same memory address. Is there a way to achieve this? In C I would achieve this with a pointer, or a reference in C++. Something like (in pseudo-code, now):

```julia
type MyField
    ptr_to_coord::(&Coord)
    val::Array{Real}
    name::AbstractString
end

```

where ptr\_to\_coord would be a pointer to the memory address of Coord… Is there a Julia-way of doing this, or am I thinking about this the wrong way?

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 5, 2017, 4:55pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/2 "2017-01-05T16:55:07Z")

</div>

> [@mzilhao](#):
>
> My understanding is that both A and B now contain two copies of my Coord type, right? ie, A.coord and B.coord are independent and are each occupying space in memory.

No, `A.coord` and `B.coord` are the same object:

```julia
julia> A.coord === B.coord
true

```

---

<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:** [January 5, 2017, 5:43pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/3 "2017-01-05T17:43:47Z")

</div>

> [@simonbyrne](#):
>
> No, A.coord and B.coord are the same object:

`===` returns `true` because `Coord` is `immutable`, and hence is defined by its value. They are actually stored separately in memory. If you want them to be the same object in memory, just use `type` rather than `immutable`.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 5, 2017, 6:22pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/4 "2017-01-05T18:22:07Z")

</div>

Well, in this case the majority of the memory is the array, and indeed they both use the same array, so the effect is the same.

---

<div class="post-metadata">

**Author:** ![mzilhao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzilhao/32/9473_2.png) [@mzilhao](https://discourse.julialang.org/u/mzilhao)\
**Post date:** [January 5, 2017, 8:16pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/5 "2017-01-05T20:16:30Z")

</div>

So, `A.coord` and `B.coord` are stored separately, but `A.coord.val` and `B.coord.val` point to the same memory address, is that it?  
How can I know and/or check when this happens?

---

<div class="post-metadata">

**Author:** ![gragusa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gragusa/32/3547_2.png) [@gragusa](https://discourse.julialang.org/u/gragusa)\
**Post date:** [January 5, 2017, 8:25pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/6 "2017-01-05T20:25:48Z")

</div>

Try

```julia
x.val[1] = 1.0

A.coord.val
B.coord.val

```

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 5, 2017, 8:56pm UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/7 "2017-01-05T20:56:35Z")

</div>

> [@mzilhao](#):
>
> So, `A.coord` and `B.coord` are stored separately, but `A.coord.val` and `B.coord.val` point to the same memory address, is that it?

Well, with immutable objects things can be a bit funny. They could point to the same memory space, they could point to different spaces, or they may not exist at all (the compiler may decide it doesn’t need to bother creating the objects and simply keeps the elements in the fields). This is why for instance you can’t attach finalizers to immutable objects.

But yes, they will share the same array.

> [@mzilhao](#):
>
> How can I know and/or check when this happens?

You can use `===` on the arrays:

```julia
A.coord.val === B.coord.val

```

(arrays are one of the few builtin objects in Julia, but they act like mutable objects so are equal if and only if they point to the same memory).

---

<div class="post-metadata">

**Author:** ![mzilhao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzilhao/32/9473_2.png) [@mzilhao](https://discourse.julialang.org/u/mzilhao)\
**Post date:** [January 6, 2017, 1:40am UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/8 "2017-01-06T01:40:31Z")

</div>

> [@simonbyrne](#):
>
> Well, with immutable objects things can be a bit funny. They could point to the same memory space, they could point to different spaces, or they may not exist at all (the compiler may decide it doesn’t need to bother creating the objects and simply keeps the elements in the fields). This is why for instance you can’t attach finalizers to immutable objects.
> 
> But yes, they will share the same array.

Is there a way to make sure that two given objects will be **always** pointing to the same memory space, then? something like references in C++?

---

<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:** [January 6, 2017, 1:58am UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/9 "2017-01-06T01:58:57Z")

</div>

All mutable objects (this includes arrays) are (internally) object pointers in Julia. If you assign one such object to another variable, or pass one as an argument, Julia will always just copy the pointer (the underlying object is shared in memory), equivalent to a C++ reference or object pointer.

---

<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:** [January 6, 2017, 2:00am UTC](https://discourse.julialang.org/t/create-a-type-with-pointer-to-array/1306/10 "2017-01-06T02:00:26Z")

</div>

(Sharing memory for mutable objects is required by the semantics of the language. If the memory weren’t shared, they wouldn’t have mutable semantics.)
