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?
It's because the function
initschedule
has only seen a declaration ofstruct task_struct
, not its definition. Presumably the function that callsinitschedule
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 definesthread_info
.By the time you define
initschedule
,task_struct
is still an incomplete type. You need to make sure that the whole definition ofstruct task_struct
is visible wheninitschedule
is compiled.Instead of including the header file you add the line:
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 oftask_struct
or more than the fact thattask_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:
With Incomplete type you cannot:
Proposed Solution:
The problem here because inside the function
initschedule
You try to access the members of thestruct task_struct
, namely:So the compiler here needs to know the definition of member
thread_info
. You need to include the header which definesstruct thread_info
in yourschedule.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, whileschedule.c
does.