Why am I getting this error: dereferencing pointer

2020-07-29 17:31发布

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?

3条回答
We Are One
2楼-- · 2020-07-29 17:57

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.

查看更多
一夜七次
3楼-- · 2020-07-29 17:58

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.

查看更多
爷的心禁止访问
4楼-- · 2020-07-29 18:04

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.

查看更多
登录 后发表回答