我的任务是编写my_free()和my_malloc()函数。
但是,我们如何创造结束语和序曲,以正确地对齐页眉和页脚?
据推测,我们使用堆中的sbrk() requries 4096 bytes。
我需要吗?
void* my_malloc(size_t size)
{
void* heapspace = sbrk();
heapspace += 8 // ?? Do i do this to create epilogue?
}发布于 2021-11-08 09:31:11
heapspace += 8; 不会对齐任何东西。如果需要8字节对齐,并且将8添加到未对齐地址,则结果将是相同的未对齐。
你需要:
void *align(void *ptr, unsigned alignment)
{
uintptr_t addr = (uintptr_t)ptr;
if(addr % alignment)
{
addr = ((addr / alignment) + 1) * alignment;
}
return (void *)addr;
}void* my_malloc(size_t size)
{
char* heapspace = sbrk();
heapspace += sizeof(size); //example header size
heapspace = align(heapspace, 8);
}https://stackoverflow.com/questions/69880561
复制相似问题