首页 > 解决方案 > C无法将函数指针类型识别为相同

问题描述

在最近的一个项目中,我在过去一个小时左右一直在努力修复这个语法错误;gcc 抱怨它所说的两个相同类型的对象之间的类型不匹配:

../../devices/timer.c: In function ‘timer_interrupt’:
../../devices/timer.c:181:19: warning: passing argument 1 of ‘thread_foreach’ from incompatible pointer type [-Wincompatible-pointer-types]
  181 |   thread_foreach (timer_check, NULL);
      |                   ^~~~~~~~~~~
      |                   |
      |                   void (*)(struct thread *, void *)
In file included from ../../devices/timer.c:9:
../../threads/thread.h:134:42: note: expected ‘void (*)(struct thread *, void *)’ but argument is of type ‘void (*)(struct thread *, void *)’
  134 | void thread_foreach (thread_action_func *func, void *aux);
      |                      ~~~~~~~~~~~~~~~~~~~~^~~~
../../devices/timer.c: At top level:
../../devices/timer.c:185:1: error: conflicting types for ‘timer_check’
  185 | timer_check (struct thread *thread, void *aux) {
      | ^~~~~~~~~~~
In file included from ../../devices/timer.c:1:
../../devices/timer.h:29:6: note: previous declaration of ‘timer_check’ was here
   29 | void timer_check(struct thread *thread, void *aux);
      |      ^~~~~~~~~~~

我玩过诸如添加/删除引用或取消引用运算符之类的事情,更改所涉及函数的一些签名以使它们与我在网上看到的示例更相似。

例如,我尝试更改thread_action_funcfrom typedef void thread_action_functo的签名typedef void (*thread_action_func),并将函数参数中类型的使用从 to 更改thread_action_func *thread_action_func,但它要么抱怨传递的类型不再是函数或函数指针,要么抛出关于相同的相同错误类型不匹配。

我还尝试在函数thread_foreach调用的位置前面添加一个地址timer_check作为参数,例如thread_foreach(&timer_check,...),但错误与最初相同。

相关的函数/原型/类型定义是:

线程.h:

struct thread
  {
    ...
    int64_t block_ticks;
    ...
  };

typedef void thread_action_func (struct thread *t, void *aux);
void thread_foreach (thread_action_func *func, void *aux);

线程.c:

void
thread_foreach (thread_action_func *func, void *aux)
{
  struct list_elem *e;

  ASSERT (intr_get_level () == INTR_OFF);

  for (e = list_begin (&all_list); e != list_end (&all_list);
       e = list_next (e))
    {
      struct thread *t = list_entry (e, struct thread, allelem);
      func (t, aux);
    }
}

计时器.h:

void timer_check(struct thread *thread, void *aux);

计时器.c:

void
timer_sleep (int64_t ticks) 
{
  int64_t start = timer_ticks ();

  ASSERT (intr_get_level () == INTR_ON);

  struct thread *cur = thread_current();
  cur->block_ticks = ticks;
  enum intr_level old_level = intr_disable ();
  thread_block();

  intr_set_level (old_level);
  thread_yield();
}

static void
timer_interrupt (struct intr_frame *args UNUSED)
{
  ticks++;
  thread_tick ();
  thread_foreach (timer_check, NULL);
}

void 
timer_check (struct thread *thread, void *aux) {
  if (thread->status == THREAD_BLOCKED) {
    thread->block_ticks--;
    if (thread->block_ticks <= 0) {
      thread_unblock(thread);
    }
  }
}

我通过搜索此类问题找到的所有结果都只是在一个方面或另一个方面相似,不够接近以至于没有用处,例如显示函数指针的示例是什么或指向整数或字符的指针的错误。

我猜这是一些明显的语法错误,我太沮丧了,没有注意到,但我现在无法真正清楚地看到可能导致问题的原因。

标签: cmultithreadingoperating-systemfunction-pointerspintos

解决方案


struct当您在不同的范围内重新定义标记时,会发生此错误。

您尚未提供Minimal Reproducible Example,因此我们无法明确确定错误所在,但可能是以下代码timer.h

void timer_check(struct thread *thread, void *aux);

struct thread用函数原型范围声明。这仅在函数声明期间定义了一个struct类型,这通常是无用的。

然后,在 中timer.c,大概你包含了一些声明的头文件thread_unblock,并且这个头文件被包含在timer.h被包含之后(如果是的话)。该标头声明了它自己的struct thread并在thread_unblock.

由于该函数timer_check采用的参数类型与 中使用的参数类型不同thread_unblock,因此编译器报告它们具有不兼容的类型,即使它们具有相同的名称(来自不同的范围)。

要解决此问题,timer.h应在声明struct thread之前包含声明的标头timer_check。然后参数声明struct thread *thread将引用已经可见的结构类型,而不是定义一个新的。

为了说明,这里是重现错误的代码:

// struct thread; // Uncommenting this line will elimninate the error.

void foo(struct thread *);
void bar(void (*)(struct thread *));
void baz(void)
{
    bar(&foo);
}

推荐阅读