问题
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