v/vlib/v/tests/valgrind/strings_and_arrays.vv

33 lines
733 B
V

import os
fn return_array(array_arg []string) []int { // array argument must not be freed
s := [1, 2, 3] // escaping array must not be freed
return s
}
fn foo() {
nums := [1, 2, 3] // local array must be freed
println(nums)
nums_copy := nums // array assignments call .clone()
println(nums_copy)
name := 'Peter' // string literals mustn't be freed
str_inter := 'hello, $name' // concatenated strings must be freed
// nums.free() // this should result in a double free and a CI error
}
fn str_replace() {
s := 'hello world'
r := s.replace('hello', 'hi')
cloned := s.replace('hello', 'hi').clone()
cloned2 := r.clone()
println(s)
println(r)
}
fn main() {
println('start')
foo()
// str_replace()
println('end')
}