Linux 为什么我会收到此错误:取消引用指向不完整类型的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8000739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:00:00  来源:igfitidea点击:

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

clinuxscheduling

提问by shuniar

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

我正在开发一个课程项目,其中可能是最糟糕的指令,我们必须实现一个简单的调度程序..虽然 C 编程不是课程的先决条件,但这是调度程序的语言,我不一定是 C程序员..

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

schedule.c:61:48: 错误:取消引用指向不完整类型的指针

This is the task_structdefinition:

这是task_struct定义:

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 seedTaskis passed into.

最奇怪的是,当我printf(...)在调用上述函数的方法中使用相同的方法时,它可以工作,但只是在我seedTask传入的函数中不起作用。

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

所以基本上,我想知道为什么我会收到这个错误?

采纳答案by Alok Save

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 declaresthe class task_structwhich 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_structis 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.

Forward 声明了类task_struct,这意味着对于编译器它是一个Incomplete 类型。对于不完整的类型,一个人不能创建它的变量或做任何需要编译器知道布局task_structtask_struct不仅仅是类型这一事实的事情。即:编译器不知道它的成员是什么以及它的内存布局是什么。
但是由于指向所有对象的指针只需要相同的内存分配,因此您可以在将 Incomplete 类型作为指针引用时使用前向声明。

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:

前向声明对于如何进一步使用 Incomplete 类型有其自身的限制。
使用 Incomplete 类型,您可以:

  • 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:

使用 Incomplete 类型,您不能:

  • Use it to declare a member.
  • Define functions or methods using this type.
  • 用它来声明一个成员。
  • 使用此类型定义函数或方法。

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

建议的解决方案:
这里的问题是因为在函数内部initschedule你尝试访问 的成员struct task_struct,即:

(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_infoin your schedule.c.

所以这里的编译器需要知道 member 的定义thread_info。您需要包含struct thread_info在您的schedule.c.

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

为什么它在 中没有错误schedule.h
因为该头文件仅将 引用struct thread_info为指针,而没有引用其成员,而 whileschedule.c确实如此。

回答by cnicutar

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

这是因为函数initschedule只看到了 的声明struct task_struct而不是它的定义。据推测,调用的函数initschedule已经看到了定义,因此可以取消引用指向它的指针。

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

所以对于initschedule,它是一个不完整的类型,它只能传递指向它的指针,但实际上不能解引用这些指针。

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

只需确保在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.

现在看来,这是导致此另一个不完整的定义:thread_info。这与上面解释的基本问题相同,但您必须包含定义thread_info.

回答by K-ballo

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

到您定义时initschedule, ,task_struct仍然是一个不完整的类型。您需要确保struct task_structinitschedule编译时整个定义是可见的。