Memory Access Crashes in Playdate Game Developed with Zig

I’m going to point you to this awesome post by @squeek502: Is it Ok to use std.heap.page_allocator directly? - #7 by squeek502

To answer your question, resize does not require a new pointer. It asks for a bigger chunk of memory in place. Let’s go back to that code example I posted:

            if (self.allocator.resize(old_memory, new_capacity)) {
                self.capacity = new_capacity;
                return addManyAtAssumeCapacity(self, index, count);
            }

So if resize works, it does not reassign the pointer - it just increases the self.capacity meaning that the array list can take in more items with its current allocation position. In the second half, you’ll see this:

            const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
            // removed for sake of example...
            self.allocator.free(old_memory);
            self.items = new_memory[0..new_len];
            self.capacity = new_memory.len;

So it gets new memory, and then overwrites it’s current items member (which is the slice of used items) and also assigns its extra capacity.

If you fail to resize then you need a new pointer. In that case, you need to return false because it failed, signalling you to do something else to get the memory you need.