警告:“noreturn"函数确实返回 [英] warning: ‘noreturn’ function does return

查看:78
本文介绍了警告:“noreturn"函数确实返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a thread library (changing context with uncontext.h). My function is of type void, and I can't return. But even if I do not return, this warning appears when compiling:

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

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

This is a simplified code of the function (without some details):

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 给了我这个警告.有谁知道是什么问题?

Note that I don't return anytime. But gcc give me this warning. Does anyone know what's the problem?

推荐答案

即使 void 函数返回,它也只是不返回 value.return; 意味着它返回到它在前一个函数中调用的地方,有或没有新值.正如@Matthias 之前所说,任何函数都会在 C 的末尾自动返回.如果编译器到达函数的结束括号,它将返回.我相信您需要使用另一个函数调用或类似的方法离开该函数以消除警告.

Even a void function returns, it just doesn't return a value. A return; means that it goes back to where it was called in the previous function, with or without a new value. Any function will have an automatic return at the end in C, as @Matthias said previously. If the compiler reaches the end bracket of the function, it will return. I believe you need to leave the function with another function call or something similar to get rid of the warning.

我希望这会有所帮助.

这篇关于警告:“noreturn"函数确实返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆