[C++] Why can't I find the address of the block's header in my custom allocator?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by StackMasher, Jun 26, 2017.

  1. StackMasher
    OP

    StackMasher GBAtemp Regular

    Member
    111
    55
    Nov 29, 2016
    Here's the source code: https://github.com/RealPipeline/Paw/tree/master/src/memory
    Basically, in the alloc function I return the blocks header + blockMetaSize, which is the size of the block header plus any extra bytes added in for alignment
    In the free function, I try to get the header's address again by taking blockMetaSize from the address passed, but the address is incorrect
    Here's some debug output:
    Code:
    in alloc()
    0x7faf8ad70c20 (the address of the data to return), 64 (blockMetaSize), 0x7faf8ad70020 (header address)
    In free()
    0x7faf8ad70c20 (the address of the data to free (correct)), 64 (blockMetaSize), 0x7faf8ad70b20 (calculated header address (wrong by b00!)
     
  2. StackMasher
    OP

    StackMasher GBAtemp Regular

    Member
    111
    55
    Nov 29, 2016
    Converting each pointer to uintptr_t rather than converting the whole expression to uintptr_t fixed it for some reason, I'm not sure why but it works
    solved