I have this struct:
typedef struct chunk
{
int size;
int available;
struct chunk* next;
} chunk;
我通过以下方式初始化一个节点:
chunk* head, ptr;
chunk* node = (chunk*) brkOrigin;
node->size = alloc - sizeof(chunk);
node->available = 1;
node->next = NULL;
我没有使用malloc(),因为这是一个任务,我必须要实现myMalloc()函数,所以在那段代码之前,我使用sbrk()得到了brkOrigin的地址。这就是为什么我使用直接地址而不是malloc()。但是我不知道这样做是否正确,如果有人知道如何初始化一个链表节点而不使用malloc(),那就太棒了。
但我必须搜索链表,当我尝试这样做时出现了一些错误:
head = node;
ptr = head;
while(ptr != NULL)
{
if(ptr->size >= mem && ptr->available == 1)
{
ptr->available = 0;
if(ptr->size > mem)
{
//Split in two nodes. Basically, create another with the remainder of memory.
}
}
else
ptr = ptr->next;
}
错误信息:
error: incompatible types when assigning to type ‘chunk’ from type ‘struct chunk *’
ptr = head;
error: invalid operands to binary != (have ‘chunk’ and ‘void *’)
while(ptr != NULL)
error: invalid type argument of ‘->’ (have ‘chunk’)
if(ptr->size >= mem && ptr->available == 1)
error: invalid type argument of ‘->’ (have ‘chunk’)
if(ptr->size >= mem && ptr->available == 1)
error: invalid type argument of ‘->’ (have ‘chunk’)
ptr->available = 0;
error: invalid type argument of ‘->’ (have ‘chunk’)
if(ptr->size > mem)
error: invalid type argument of ‘->’ (have ‘chunk’)
ptr = ptr->next;
非常抱歉,如果这是一个愚蠢的问题(或者一个愚蠢的错误),这是我第一次(积极地)使用Stack Overflow。我无法理解这些错误。但我几乎确定问题在于未使用malloc()初始化节点...