Skip to content

Commit 98c3f4c

Browse files
Update conversions_views.md (#170)
Added one more example for understanding better how rawview() of an array is just a reference to same object in a different representation. Co-authored-by: Johnny Chen <[email protected]>
1 parent 16a6d42 commit 98c3f4c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/src/tutorials/conversions_views.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,31 @@ julia> a
229229
1.0N0f8
230230
```
231231

232+
We can understand clearly from below code that `v` is an immutable object, or is just
233+
a reference of `a`. It doesn't has a separate memory allocated to it:
234+
235+
```jldoctest; setup = :(using Images)
236+
julia> a = [0.2N0f8,0.8N0f8]
237+
2-element Array{N0f8,1} with eltype Normed{UInt8,8}:
238+
0.2N0f8
239+
0.8N0f8
240+
241+
julia> v = rawview(a)
242+
2-element reinterpret(UInt8, ::Array{N0f8,1}):
243+
0x33
244+
0xcc
245+
246+
julia> pointer_from_objref(a) #function used to find address of an object
247+
Ptr{Nothing} @0x00000000144adc90
248+
249+
julia> pointer_from_objref(v) #v is just a immutable reference to a, no separate memory allocated to it.
250+
ERROR: pointer_from_objref cannot be used on immutable objects
251+
Stacktrace:
252+
[1] error(::String) at .\error.jl:33
253+
[2] pointer_from_objref(::Any) at .\pointer.jl:146
254+
[3] top-level scope at none:1
255+
```
256+
232257
The opposite transformation is `normedview`:
233258

234259
```jldoctest; setup = :(using Images)

0 commit comments

Comments
 (0)