Memory Issues: Serializing and deserializing data

Using julia v0.6.4

I was trying to convert String to bytes and add it to a buffer. I noticed the following

a = “temp”
b = Vector{UInt8}(a)
b[1] += 1

This ends up modifying a. Shouldn’t a call to Vector create new underlying memory? What happens if a goes out of scope now?

c = String(b)
b[1] += 1

Again, c in modified. This means c also shares the underlying data. If I now apply shift! on b, what happens to the memory of c? This behavior seems similar to reinterpret for arrays, although there shift! is forbidden and one is greeted with an error, cannot resize array with shared data. Should this not behave similarly?

Please suggest proper way to handle these conversions.
Thank you

Julia 0.7:

julia> a = "temp"
"temp"

julia> b = Vector{UInt8}(a)
┌ Warning: Vector{UInt8}(s::String) will copy data in the future. To avoid copying, use `unsafe_wrap` or `codeunits` instead.
│   caller = top-level scope at none:0
└ @ Core none:0
4-element Array{UInt8,1}:
 0x74
 0x65
 0x6d
 0x70

julia> b[1] += 1
117

julia> c = String(b) # resets `b` to have 0 length
"uemp"

julia> b[1] += 1
ERROR: BoundsError: attempt to access 0-element Array{UInt8,1} at index [1]
Stacktrace:
 [1] getindex(::Array{UInt8,1}, ::Int64) at ./array.jl:731
 [2] top-level scope at none:0
1 Like

Ok, so essentially these issues have been fixed in julia-0.7. Thanks for the reply.

1 Like