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)
3 Replies
List(capacity=n)
only allocates the memory but doesn't actually initialize it so it still has a size
of 0 and indexing into those elements is an out of bounds access. If you want to bulk initialize a list you want List.resize
And resize calls
__copyinit__
for each entry? Is there a more performant way of just initializing space if you are going to set the values right after?I think the append based version is what you would want for something like this