#what is the stupid thing that i am doing here related to allocations

1 messages · Page 1 of 1 (latest)

sudden patrol
#
pub fn init(allocator: Allocator) Self {
    const lorem = allocator.create(std.AutoHashMapUnmanaged(u8, Lorem)) catch @panic("OOM!");
    lorem.* = .empty;
    lorem .*.ensureTotalCapacity(allocator, 15) catch @panic("OOM!");

    return Self{
        .lorem = lorem,
    };
}

pub fn deinit(self: Self, allocator: Allocator) void {
    self.lorem.deinit(allocator);
    allocator.destroy(self.lorem);
}

causes an incorrect pointer alignment in self.lorem.deinit(allocator);

obsidian anchor
#

That example can't be complete, you never store loremin self. But I assume with the complete example, the issue is with this call: lorem .*.ensureTotalCapacity the lorem.* is creating a copy which, I assume, is a different value than what you're storing in Self and ultimately freeing.

ember geyser
sudden patrol
#

i forgot to rename

sudden patrol
modern lotus
#

just have the players field not be a pointer type

#

and do break :blk players

#

you dont have to store a hashmap on the heap since it already stores its dynamically sized data on the heap, you can copy around the stack stuff just fine as long as you treat it like a “move”

sudden patrol
# modern lotus just have the players field not be a pointer type

sorry for the late response, got quite busy with other things, would it not be possible to store it on the heap anyways? currently if i want to store it as a non pointer field, i have to make a bunch of changes just so i could get it to deinitialize correctly

#

this is because the whole struct is contained in a fixed size slice, so think of it like

const ParentStruct = struct {
  child_struct: [SOME_CONSTANT]ChildStruct,

  pub fn deinit(self: ParentStruct , allocator: Allocator) void {
    inline for (self.child_struct) |child| {
        child.deinit(allocator);
    }
}
}

turning *std.AutoHashMapUnmanaged(u8, Lorem) -> std.AutoHashMapUnmanaged(u8, Lorem) from inside ChildStruct leads to not being able to deinitialize because of *const ChildStruct, which basically as far as i understand leads to me more or less being unable to statically allocate the slice