Linked list node initialization without using malloc ()

I have this structure:

typedef struct chunk
{
  int size;
  int available;
  struct chunk* next;
} chunk;

      

I am initializing one node like this:

chunk* head, ptr;

chunk* node = (chunk*) brkOrigin;
node->size = alloc - sizeof(chunk);
node->available = 1;
node->next = NULL;

      

I don't use malloc () because this is the assignment I have to implement myMalloc (), so brkOrigin is the address I got with sbrk (), before this piece of code. This is why I am using this direct address instead of malloc (). But I don’t know if it’s the right thing to do, if anyone has an idea on how to initialize a node of a favorite list without malloc () it would be nice as well.

But I have to search for a linked list and I got some errors when trying:

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;
}

      

Errors:

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;

      

Sorry if this is a silly question (or a silly mistake), this is my first time using (actively) stack overflow. I don't understand these errors. But I'm pretty sure there is a problem initializing node without malloc () ...

+3


source to share


1 answer


chunk* head, ptr

doesn't do what you think it does. This is equivalent to:

chunk *head;
chunk ptr;

      

You also want:

chunk *head;
chunk *ptr;

      



or, if you insist on one line:

chunk *head, *ptr;

      

Here's a link to your problem in the C> FAQ. There are more comments and details available from there.

+8


source







All Articles