You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following example is incorrect. Running the following code gives me the output of
a -> [2, 2, 2, 2, 2, 3, 4]
b -> [2, 3, 13]
mut a := []int{len: 5, cap: 6, init: 2}
mut b := a[1..4]
a << 3
// no reallocation - fits in `cap`
b[2] = 13 // `a[3]` is modified
a << 4
// a has been reallocated and is now independent from `b` (`cap` was exceeded)
b[1] = 3 // no change in `a`
println(a) // `[2, 2, 2, 13, 2, 3, 4]`
println(b) // `[2, 3, 13]`
Updated code - requires the unsafe
mut a := []int{len: 5, cap: 6, init: 2}
mut b := unsafe { a[1..4] }
a << 3
// no reallocation - fits in `cap`
b[2] = 13 // `a[3]` is modified
a << 4
// a has been reallocated and is now independent from `b` (`cap` was exceeded)
b[1] = 3 // no change in `a`
println(a) // `[2, 2, 2, 13, 2, 3, 4]`
println(b) // `[2, 3, 13]`
Describe the issue
The following example is incorrect. Running the following code gives me the output of
a -> [2, 2, 2, 2, 2, 3, 4]
b -> [2, 3, 13]
Updated code - requires the unsafe
Links
https://github.com/vlang/v/blob/master/doc/docs.md#array-slices
The text was updated successfully, but these errors were encountered: