Let’s say I have the program like this:
pub const LInt = std.ArrayList(i32);
// function main
const lst1 = flx.LInt.init(allocator);
const lst2 = flx.LInt.init(allocator);
std.debug.print("{any}\n", .{lst1});
std.debug.print("{any}\n", .{lst2});
Variables lst1
and lst2
will point to the same memory address. Can someone explain to me why? How do I create two isolated instances?
Edit: Sorry, my previous code doesn’t reproduce my error (I thought it would). This is the code that will generate/get the same array list.
pub const LInt = std.ArrayList(i32);
pub const IntLInt = std.HashMap(i32, *LInt, std.hash_map.AutoContext(i32), std.hash_map.default_max_load_percentage);
/// Util function leverage hashmap
pub fn dictInsert(allocator: std.mem.Allocator, dict: *flx.IntLInt, key: i32, val: i32) !void {
if (!dict.contains(key)) {
var lst = flx.LInt.init(allocator);
std.debug.print("created!! {} @ {*}\n", .{key, &lst});
try dict.put(key, &lst);
}
try dict.getPtr(key).?.*.insert(0, val);
}
/// main
var str_info = IntLInt.init(allocator); // just a hashmap
// insert a bunch of random data
try dictInsert(allocator, &str_info, 0, 10);
try dictInsert(allocator, &str_info, 0, 99);
try dictInsert(allocator, &str_info, 1, 7);
try dictInsert(allocator, &str_info, 1, 55);
try dictInsert(allocator, &str_info, 1, 4);
try dictInsert(allocator, &str_info, 0, 99);
Output:
created!! 0 @ array_list.ArrayListAligned(i32,null)@a2229ff670
created!! 1 @ array_list.ArrayListAligned(i32,null)@a2229ff670
I was expecting each key
map to an ArrayList
, but I was getting the same instance.