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

joseph picture joseph · Jun 20, 2013 · Viewed 22.3k times · Source

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 ?

Answer

Dayal rai picture Dayal rai · Jun 20, 2013

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.