ModularM
Modular16mo ago
28 replies
blblblbl

How do safe Reference and List work together?

From my understanding Reference is just a pointer in memory with associated lifetime. But I don't understand how the following code doesn't crash
    var list: List[Int] = List(10,11,12)
    var element = Reference(list[0])
    list.resize(0)
    for i in range(10000):
         list.append(i)
    print(element[])

Certainly reallocation should have happened here and therefore element should point to invalid memory location, which should cause segfault. But it prints
0
. What am I missing?
Was this page helpful?