3

I have this struct:

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

I initialize one node doing this:

chunk* head, ptr;

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

I'm not using malloc(), because this is an assignment which I have to implement myMalloc(), so brkOrigin is an address I got using sbrk(), before that piece of code. This is why I'm using this direct address instead of malloc(). But I don't know if it is correct to do like this, if someone has some idea on how to initialize a node of a liked list without malloc(), it would be nice too.

But I have to search the linked list, and I got some errors when trying this:

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

The 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 dumb question (or a dumb mistake), this is my first time using (actively) Stack Overflow. I can't understand this errors. But I'm almost sure the problem is with the node initialization without malloc()...

1 Answer 1

8

chunk* head, ptr isn't doing what you think it's doing. It's equivalent to:

chunk *head;
chunk ptr;

What you want is either:

chunk *head;
chunk *ptr;

or, on one line if you insist:

chunk *head, *ptr;

Here's a link to exactly your problem at the C FAQ. There are more commentaries and details available from there.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh! Thank you for the clear explanation, helped a lot! Besides that, the initialization of the node without using malloc() is ok?
I don't think there's enough code there to say for sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.