Jake
Jake
MModular
Created by Jake on 1/23/2025 in #questions
List setItem vs append crashing?
Below I have two simple Matrix structs. They are the exact same except the lines called out by # <- comments. Why is the SetMatrix causing a crash while AppendMatrix is fine? (Run on https://docs.modular.com/mojo/playground for a quick repro)
struct AppendMatrix():
var value: List[List[Float64]]
var rows: Int
var cols: Int

fn __init__(out self, rows: Int, cols: Int):
self.rows = rows
self.cols = cols
self.value = List[List[Float64]](capacity = rows)
for i in range(rows):
self.value.append(List[Float64](capacity = cols)) # <- appending works fine
for j in range(cols):
self.value[i].append(10*i + j) # <- appending works fine

struct SetMatrix():
var value: List[List[Float64]]
var rows: Int
var cols: Int

fn __init__(out self, rows: Int, cols: Int):
self.rows = rows
self.cols = cols
self.value = List[List[Float64]](capacity = rows)
for i in range(rows):
self.value[i] = List[Float64](capacity = cols) # <- assigning causes a crash?
for j in range(cols):
self.value[i][j] = (10*i + j) # <- assigning is fine here

fn main():
print("Matrices!", flush = True)

for i in range(1, 10):
var A = AppendMatrix(i, 3)
print("A" + str(i), A.rows, "x", A.cols, flush = True)

for i in range(1, 10):
var B = SetMatrix(i, 3) # <- this will crash
print("B" + str(i), B.rows, "x", B.cols, flush = True)
struct AppendMatrix():
var value: List[List[Float64]]
var rows: Int
var cols: Int

fn __init__(out self, rows: Int, cols: Int):
self.rows = rows
self.cols = cols
self.value = List[List[Float64]](capacity = rows)
for i in range(rows):
self.value.append(List[Float64](capacity = cols)) # <- appending works fine
for j in range(cols):
self.value[i].append(10*i + j) # <- appending works fine

struct SetMatrix():
var value: List[List[Float64]]
var rows: Int
var cols: Int

fn __init__(out self, rows: Int, cols: Int):
self.rows = rows
self.cols = cols
self.value = List[List[Float64]](capacity = rows)
for i in range(rows):
self.value[i] = List[Float64](capacity = cols) # <- assigning causes a crash?
for j in range(cols):
self.value[i][j] = (10*i + j) # <- assigning is fine here

fn main():
print("Matrices!", flush = True)

for i in range(1, 10):
var A = AppendMatrix(i, 3)
print("A" + str(i), A.rows, "x", A.cols, flush = True)

for i in range(1, 10):
var B = SetMatrix(i, 3) # <- this will crash
print("B" + str(i), B.rows, "x", B.cols, flush = True)
4 replies
MModular
Created by Jake on 1/21/2025 in #questions
Installing via Magic script
I am following https://docs.modular.com/mojo/manual/get-started/ and using a devcontainer in the process. (See https://github.com/jake-danton/mojo_test for reference). I got it mostly working, but had some additional dependencies to install (build-essential, etc) to go from a clean Ubuntu setup (see repro for more info). So there may need to be either prerequisites added to the docs or an update to the script. The two big things I haven't got working are: * LLVM_SYMBOLIZER_PATH not being set (this should happen during installation, right?) * The elf_dynamic_array_reader.h:64] tag not found error that many GitHub issues call out Any insights to fixing these would be super helpful! Thanks!
15 replies