我正在编写软件来模拟“first-fit”内存分配模式。
基本上,我分配了一大块 X 兆字节的内存,并在根据架构请求块时将其细分为块。
我使用一个名为“node”的链表作为每个内存块的标题(这样我们就可以找到下一个块,而无需繁琐地循环遍历每个地址值。
head_ptr = (char*) malloc(total_size + sizeof(node));
if(head_ptr == NULL) return -1; // Malloc Error .. :-(
node* head_node = new node; // Build block header
head_node->next = NULL;
head_node->previous = NULL;
// Header points to next block (which doesn't exist yet)
memset(head_ptr,head_node, sizeof(node));
`
但是最后一行返回:
error: invalid conversion from 'node*' to 'int'
我明白为什么这是无效的..但是我怎样才能将我的节点放入我新分配的内存的指针位置?