- I have limited experience with C, and this college assignment is the perfect way to get good at it. I have to design a heap that implements the memory allocation and free function.
- I'm confused how doing `ptr + 1` skips 4 bytes
- This actually depends on the size of the data that the pointer is pointing to
- `p = p + (1 * sizeof(*p));` this is a better way of looking at it
- Side note: I don't think software engineering is fitting for this topic but whatever I'll just go with it right now
- The header struct that I have been given cares about the following:
- Bit 0 (least significant bit, LSB)
- 0 -> free block
- 1 -> allocated block
- Bit 1 (second least significant bit)
- 0 -> previous block free
- 1 -> previous block allocated
- So I have 4 options for the status bits:
- 00 -> this block and previous block are free
- 01 -> this block is allocated, previous block is free
- 10 -> this block is free, previous block allocated
- 11 -> this block is allocated, previous block allocated
- How will I extract the status bits?
- For my initial memory allocation (without freeing) implementation, I only need to worry about the LSB. How will I extract that bit? I can just AND it with `int mask_bit = 1;`
- I will need a header_size global variable because that basically remains constant. In my case, it is 4 bytes because the header struct only stores an `int` which contains both the size and status. How will I extract the size? (`& ~3`) How will I extract the status?
- I do not need to worry about the previous block status for the initial implementation, as that is relevant for coalescing. My first implementation only requires me to consider splitting and setting of the LSB.
- Need to ensure that a heap block size is a multiple of 8
- This is trivial.
- I wrote these things down to get my thoughts out while writing the program. Also, gdb is great. And this heap design was like 20 lines of code for allocation. I will implement freeing with coalescing at a later stage. It was way easier than I initially imagined.
### Freeing and Coalescing