warning: ‘noreturn’ function does return

南楼画角 提交于 2019-12-13 05:47:20

问题


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);


}

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


回答1:


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.

I hope this helps.




回答2:


At the end of code C inserts an implicit return. A no-return function is expected to run in a loop or exit by system call. It is different from a void function that returns but does not deliver a value.




回答3:


Just because you don't have a return doesn't mean your routine can't return. Falling off the end (control reaching the final close-brace) is the equivalent of return.

Thus

foo(x)
{
    ...
}

is the same as

foo(x)
{
    ...
    return;
}


来源:https://stackoverflow.com/questions/23452489/warning-noreturn-function-does-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!