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

限于喜欢 提交于 2019-12-10 19:59:05

问题


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 ?


回答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.




回答2:


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

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

Use this,

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



回答3:


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.



来源:https://stackoverflow.com/questions/17211345/invalid-application-of-sizeof-to-incomplete-type-list-struct-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!