【问题标题】:warning: ‘noreturn’ function does return警告:“noreturn”函数确实返回
【发布时间】:2014-05-04 12:24:07
【问题描述】:

我正在做一个线程库(使用 uncontext.h 更改上下文)。 我的函数是 void 类型,我无法返回。但是即使我不返回,编译时也会出现这个警告:

dccthread.c: In function ‘dccthread_init’:
dccthread.c:184:1: warning: ‘noreturn’ function does return [enabled by default]
 }

这是函数的简化代码(没有一些细节):

void dccthread_init(void (*func), int param) {
    int i=0;
    if (gerente==NULL)
    gerente = (dccthread_t *) malloc(sizeof(dccthread_t));

    getcontext(&gerente->contexto);
    gerente->contexto.uc_link = NULL;
    gerente->contexto.uc_stack.ss_sp = malloc ( THREAD_STACK_SIZE );
    gerente->contexto.uc_stack.ss_size = THREAD_STACK_SIZE;
    gerente->contexto.uc_stack.ss_flags = 0;
    gerente->tid=-1;

    makecontext(&gerente->contexto, gerente_escalonador, 0);
    if (principal==NULL)
    principal = (dccthread_t *) malloc(sizeof(dccthread_t));

    getcontext(&principal->contexto);
    principal->contexto.uc_link = NULL;
    principal->contexto.uc_stack.ss_sp = malloc ( THREAD_STACK_SIZE );
    principal->contexto.uc_stack.ss_size = THREAD_STACK_SIZE;
    principal->contexto.uc_stack.ss_flags = 0;
    makecontext(&principal->contexto, func, 1, param);
    swapcontext(&gerente->contexto, &principal->contexto);


}

请注意,我不会随时返回。但是 gcc 给了我这个警告。有谁知道问题出在哪里?

【问题讨论】:

    标签: c multithreading warnings ucontext


    【解决方案1】:

    即使void 函数返回,它只是不返回return; 表示它返回到在前一个函数中调用它的位置,无论是否有新值。正如@Matthias 之前所说,任何函数都会在 C 的末尾自动返回。如果编译器到达函数的结束括号,它将返回。我相信你需要用另一个函数调用或类似的东西离开这个函数来摆脱警告。

    我希望这会有所帮助。

    【讨论】:

    • 它解决了这个问题。但我不知道我将如何做到这一点。但这是一个开始,我会寻找方法。谢谢。
    • 没问题。很高兴我能帮上忙。
    【解决方案2】:

    在代码 C 的末尾插入一个隐式返回。无返回函数应在循环中运行或通过系统调用退出。 它与返回但不传递值的 void 函数不同。

    【讨论】:

      【解决方案3】:

      仅仅因为您没有return 并不意味着您的例程无法返回。从末端跌落(控制到达最后的闭括号)相当于返回。

      这样

      foo(x)
      {
          ...
      }
      

      一样
      foo(x)
      {
          ...
          return;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-04-04
        • 2016-12-09
        • 2020-01-09
        • 1970-01-01
        • 2016-08-14
        • 2016-12-01
        • 1970-01-01
        • 2020-10-03
        • 1970-01-01
        相关资源
        最近更新 更多