I have dispatchQueue.c:215: warning: control reaches end of non-void function
warning from the code below..
Can anyone please explain why?
void *dispatcher_threadloop(void *arg){
//thread loop of the dispatch thread- pass the tast to one of worker thread
dispatch_queue_thread_t *dThread = arg;
dispatch_queue_t *dQueue;
dQueue = dThread->queue;
if (dQueue->HEAD!=NULL){
for(;;){
printf("test");
sem_wait(&(dQueue->queue_task_semaphore));
dThread->current_task = dQueue->HEAD;
dQueue->HEAD = dQueue->HEAD->next;
dQueue->HEAD->prev = NULL;
sem_post(&(dQueue->queue_task_semaphore));
break;
//TODO
}
}
}
If the function is non-void,it means it has to return something before reaching the end of function block[ _} ]. So, when we give only if and else-if statements the compiler cannot tell from that code,that any of these statements will be evaluated to true and return something.
1) A Void Function Can Return: We can simply write a return statement in a void fun(). In fact, it is considered a good practice (for readability of code) to write a return; statement to indicate the end of the function.
Use return; instead of return(0); to exit a void function.
The literal meaning of void is empty or blank. In C, void can be used as a data type that represents no data.
Because you are declaring it void *
(not void
) and not returning anything. Return NULL
if you don’t need any return value.
void *dispatcher_threadloop(void *arg)
Well, imagine what happens if dQueue->HEAD
is NULL
: the if
won't be entered, so you get to the end of the function which is supposed to return a void*
- but you don't return anything.
Try returning some sensible value at the bottom of your function to fix this. Or add an assertion which states that this code should be unreachable, like:
assert( !"Unreachable code hit" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With