Why am I getting this error: dereferencing pointer to incomplete type

12,722

Solution 1

Why am I getting this error: dereferencing pointer to incomplete type?

Instead of including the header file you add the line:

struct task_struct;

It Forward declares the class task_struct which means for compiler it is an Incomplete type. With Incomplete types, One cannot create variable of it or do anything which needs the compiler to know the layout of task_structor more than the fact that task_struct is just an type. i.e: The compiler does not know what are its members and what its memory layout is.
But Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to an Incomplete type as a pointer.

Note that forward declarations are usually used in case where there is a Circular Dependency of classes.

Forward Declaration has its own limitations on how the Incomplete type can be used further on.
With Incomplete type you can:

  • Declare a member to be a pointer to the incomplete type.
  • Declare functions or methods which accepts/return incomplete types.
  • Define functions or methods which accepts/return pointers to the incomplete type (but without using its members).

With Incomplete type you cannot:

  • Use it to declare a member.
  • Define functions or methods using this type.

Proposed Solution:
The problem here because inside the function initschedule You try to access the members of the struct task_struct, namely:

(seedTask)->thread_info->processName

So the compiler here needs to know the definition of member thread_info. You need to include the header which defines struct thread_info in your schedule.c.

Why it works without error in schedule.h?
Because that header file only references the struct thread_info as an pointer and none of its member, while schedule.c does.

Solution 2

It's because the function initschedule has only seen a declaration of struct task_struct, not its definition. Presumably the function that calls initschedule has seen the definition and can therefore dereference the pointer to it.

So for initschedule, it's an incomplete type, it can only pass pointers to it around but cannot actually dereference those pointers.

Just make sure you include the header that defined that struct in schedule.c.

EDIT

It seems it's another incomplete definition that is causing this: thread_info. It's the same basic problem as explained above, but you have to include the header that defines thread_info.

Solution 3

By the time you define initschedule, task_struct is still an incomplete type. You need to make sure that the whole definition of struct task_struct is visible when initschedule is compiled.

Share:
12,722
shuniar
Author by

shuniar

I am a .NET developer and enjoy learning about and implementing new technologies.

Updated on July 25, 2022

Comments

  • shuniar
    shuniar almost 2 years

    I am working on a project for class with probably the WORST instruction ever where we have to implement a simple scheduler.. although C programming is not a prereq for the course, that is the language of the scheduler and I'm not necessarily a C programmer..

    Anyhow, I am trying to debug this by printing out the task so I can trace it through the program, but I keep getting the following compile-time error:

    schedule.c:61:48: error: dereferencing pointer to incomplete type

    This is the task_struct definition:

    struct task_struct
    {
        struct thread_info *thread_info;        
        int prio, static_prio, normal_prio;     
        unsigned long sleep_avg;            
        unsigned long long last_ran;            
        unsigned long long timestamp;           
        unsigned long long sched_time;          
        unsigned int time_slice, first_time_slice;  
        struct list_head run_list;          
        struct sched_array *array;          
        enum sleep_type sleep_type;         
        int need_reschedule;                
    };
    

    The function im trying to debug inside:

    void initschedule(struct runqueue *newrq, struct task_struct *seedTask)
    {
    printf("Inside initschedule()\n");
    
    printf("%s - TEST \n", (seedTask)->thread_info->processName); //causes compiler error
    
    /* initialize runqueue and current task */
    rq = newrq;
    current = NULL;
    
    /* allocate memory for runqueue schedule arrays */
    rq->active = (struct sched_array*)malloc(sizeof(struct sched_array));
    rq->expired = (struct sched_array*)malloc(sizeof(struct sched_array));
    
    /* initialize schedule arrays */
    INIT_LIST_HEAD(&rq->active->list);
    INIT_LIST_HEAD(&rq->active->list);
    
    /* activate task in scheduler */
    activate_task(seedTask);
    

    }

    The strangest thing is that when I use the same printf(...) in the method that calls the above function, it works, but just not in my function where the seedTask is passed into.

    So basically, I am wondering why am I getting this error?

  • shuniar
    shuniar over 12 years
    the task_struct definition is in schedule.h which is included in schedule.c which contains the initschedule function.
  • Alok Save
    Alok Save over 12 years
    @shuniar: This answer is not correct, because it suffers from the lack of information you provided in your main Q but then added here in the comment.Check my answer,It explains why the error and what is the solution.
  • cnicutar
    cnicutar over 12 years
    @Als It's just a long-winded version that also proposes including the header. I stand by my answer as it is.
  • Alok Save
    Alok Save over 12 years
    I am afraid that is Wrong. As the OP already said his schedule.c already includes schedule.h,which defines struct task_struct(Your answer suggests this very same thing).And it actually solves nothing.Because the OP needs to include the header that defines struct thread_info.
  • shuniar
    shuniar over 12 years
    Thanks, this worked after I included the privatestructs.h where the thread_info struct was defined.