The V Programming Language

Simple, fast, safe language created for developing Volt, soon available for everyone.

Open source release in June 2019. Early access since April 15.
Install V from source in 0.5 seconds
wget && gcc -o v v.c

How should I understand arrays in memory?

Apr 15 · 3 min read
SleepyRoy Jun 13 00:47
Here's a short program and its results on playground.
fn main() {
mut a := [1, 2, 3]
mut b := a
loc1 := &a
loc2 := &b
println(loc1 == loc2) // => false
b[0] = 0
println(a) // => [0, 2, 3]
println(b) // => [0, 2, 3]
The first result ("false") indicates a and b are stored at different locations in memory. However, changing b[0] affects both a and b. I'm somewhat confused about this ...
dcurrie Jun 13 01:49
I suspect the first result ("false") indicates a and b are at different locations in memory, but they happen to point to the same array. This is just like arrays in C.
medvednikov Jun 13 11:36
Yes, the same in Go.

There's an `array.clone()` method if you want a real copy. I'm not sure it's public yet, I'll make it public in V 0.0.11.

The convention is not to use operators for expensive operations (O(n) in this case).
Log in via GitHub to comment

Powered by vtalk, open-source blogging/forum software written in V