invalid application of 'sizeof' to incomplete type list struct C

23,019

Solution 1

change your struct definition as

struct pageInMemory{

            int use;
            int reference;
            int free;
            struct pageInMemory* next;
            };

to get your code working. And just for your info do not typecast void* coming from malloc.

Solution 2

It should be either:

typedef  struct pageInMemory_s {

        int use; 
        int reference;
        int free;
        struct pageInMemory_s * next;
        } pageInMemory;

pageInMemory* start, *nn, *temp, *hand;
... 
nn = malloc(sizeof(pageInMemory));

or

struct pageInMemory {

        int use; 
        int reference;
        int free;
        struct pageInMemory* next;
        };

struct pageInMemory* start, *nn, *temp, *hand;
... 
nn = malloc(sizeof(struct pageInMemory));

A third variant would be:

typedef  struct pageInMemory {

        int use; 
        int reference;
        int free;
        struct pageInMemory * next;
        } pageInMemory;

pageInMemory* start, *nn, *temp, *hand;
... 

For this 3rd option you can use:

nn = malloc(sizeof(pageInMemory));

or

nn = malloc(sizeof(struct pageInMemory));

This latter variation I feel is very irritating as there is one name for two different things:

  • The structure definition/declaration struct pageInMemory
  • The type definition/declaration pageInMemory

I would not recommend to use this 3rd option, but the second.

Solution 3

pageInMemory is itself defined as type. So instead of this,

nn = (struct pageInMemory *)malloc(sizeof(struct pageInMemory))

Use this,

nn = (pageInMemory *)malloc(sizeof(pageInMemory))
Share:
23,019
joseph
Author by

joseph

Updated on June 21, 2020

Comments

  • joseph
    joseph almost 4 years

    I'm trying to implement an replacement algorithm that deals with page faults. So i'm trying to creat a circular linked list using malloc and im getting the following error: "invalid application of sizeof' to incomplete typepageInMemory'.following is the code:

     typedef  struct {
    
                int use; 
                int reference;
                int free;
                struct pageInMemory* next;
                } pageInMemory;
    
    
                int main()
                {
    
                    int i;
                    struct pageInMemory* start, *nn, *temp, *hand;
                    start = NULL;
    
                        for(i=0; i< pNum; i++) 
                        {
                    nn = (struct pageInMemory *)malloc(sizeof(struct pageInMemory));
                            nn->use = 0;
                            nn->free = 1;
    
                            if(start==NULL)
                            {
                                nn->next = nn;
                                start =nn;
                            }
    
                            else
                            {     // sfhsdifhsdifj sdijfjsd 
                                temp = start;
                                while(temp->next != start)
                                {
                                    temp = temp->next; 
                                }
    
                                temp->next = nn;
                                nn->next = start;
                                start = nn;
    
                            }   
    
                        }
    
    
    
                    hand = start;
                    temp = start;
    
                 while(temp->next != start->next)
                 {
                    printf("%d\n", temp->use); //hi
                 } 
    
    
                    return 0;// bye
                 }  
    

    so am i not supposed to use malloc this way ?