#Array gives error when I cross the "limit"

1 messages · Page 1 of 1 (latest)

blissful gust
#

Idk why i am getting this error pls help

#

`
module main
import os {input}
fn main() {
i_max := 15
x_max := 20
mut tiles := [][]int{len:i_max, init: []int{len:x_max}}

for i:= 0; i < i_max; i+=1 {
    println("")

    for x:=0; x < x_max; x+=1 {
        if i == 0 {
            print("x")
            tiles[0][x] = 1
        }
        else if i == i_max-1 {
            print("x")
            tiles[i_max][x] = 1
        }
        else {
            if x == 0 || x == x_max-1 {
                print("x")
                tiles[i][x] = 1
            }
            else {
                print(" ")
            }
        }
    }
}
print(tiles)

}
`

#

this code returns error at line mut tiles := [][]int{len:i_max, init: []int{len:x_max}} cuz i cross the limit for the array

#

however when i edit the code to be mut tiles := [][]int{len:i_max+1, init: []int{len:x_max+1}} it doesn't give me error

#

what is weird; however, is the output

#

[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]]

#

at the first array, it ends with a 0

#

so if it ends with a 0 then why can't i just remove the +1 from the len

#

here is what the code with +1 returns `

#

xxxxxxxxxxxxxxxxxxxx
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
xxxxxxxxxxxxxxxxxxxx

#

discord f it up

#

bruh

#

here the error without +1
xV panic: array.get: index out of range (i == 15, a.len == 15) v hash: 2694624 C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:7984: at _v_panic: Backtrace C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:7236: by array_get C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:17895: by main__main C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:18288: by wmain 00494728 : by ??? 0049488b : by ??? 7ffbf86b26bd : by ???

ornate pecan
#

array indexing starts at 0

#

thus for a.len = 15, the last valid index is a[14]

blissful gust
#

Ye ik

ornate pecan
#

a[15] will produce an out of bounds panic

#

as you noticed

blissful gust
#

Where does that happen tho

ornate pecan
#

compile and run with -g

#

and you will get a backtrace with V's line numbers in it

blissful gust
#

It looks like same error

ornate pecan
#

for me, it shows that line 18 in your code is responsible

#

i.e. tiles[i_max][x] = 1

#

which indeed uses i_max as an index

#

not as a limit for the loop

blissful gust
#

OHHHH

#

how did I miss that

ornate pecan
#

not enough Vespene gas coffee

blissful gust
#

ok I fix with i_max-1

#

What is vaspene gas coffee

ornate pecan
#

it is the spice that expands consciousness for a lot of people on Earth 🙂

blissful gust
#

can I see what you got with -g

#

Cuz it gave me same error

ornate pecan
blissful gust
#

oh u gotta use flag before run

ornate pecan
#

usually you can safely ignore the lines with builtin in them and concentrate on the lines from your code

#

yes, every flag after run is passed to your program

#

every flag before run is a flag that V will try to understand