3
我有这样的结构:链表节点初始化,不使用malloc()
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()的任务,所以brkOrigin是我在使用sbrk()之前的地址,在那段代码之前。这就是为什么我使用这个直接地址而不是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;
很抱歉,如果这是一个愚蠢的问题(或哑错误),这是我第一次使用(主动)堆栈溢出。我无法理解这个错误。但我几乎可以肯定的问题是没有malloc()的节点初始化... ...
哦!感谢您的明确解释,帮助了很多!除此之外,不使用malloc()的节点的初始化可以吗? – 2014-09-27 22:11:48
我不认为那里有足够的代码可以肯定地说。 – 2014-09-28 05:17:14