c,控制到达c中非void函数的末尾

发布于 2024-11-28 19:49:59 字数 734 浏览 1 评论 0原文

我有以下代码中的 dispatchQueue.c:215: warning: controlreached end of non-void function 警告..
谁能解释一下为什么吗?

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
    }
}

}

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
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄意 2024-12-05 19:49:59

因为您声明它 void * (不是 void)并且不返回任何内容。如果不需要任何返回值,则返回NULL

void *dispatcher_threadloop(void *arg)

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)
各自安好 2024-12-05 19:49:59

好吧,想象一下如果 dQueue->HEADNULL 会发生什么:if 不会被输入,所以你会到达结尾该函数应该返回一个 void* - 但你不返回任何东西。

尝试在函数底部返回一些合理的值来解决此问题。或者添加一个断言,声明此代码应该无法访问,例如:

assert( !"Unreachable code hit" );

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" );
自演自醉 2024-12-05 19:49:59

函数的签名表明它返回一个 void *,它是一个指针,与 void 不同。

如果您的函数不返回任何内容,请使用 void

The signature for your function indicates it returns a void *, which is a pointer and is different than void.

If your function isn't meant to return anything, use void.

衣神在巴黎 2024-12-05 19:49:59

这是一个老问题,但如果有人试图解决这个问题(对于 pthreads),您可能需要返回:

  • pthread_exit(void *retval)(对于可连接线程)
  • pthread_exit(NULL) (对于分离线程)

This is an old question, but if someone's trying to figure this out (for pthreads), you MAY want to return:

  • pthread_exit(void *retval) (For joinable threads)
  • pthread_exit(NULL) (For detached threads)
墨落画卷 2024-12-05 19:49:59

http://publib.boulder.ibm.com/infocenter/tpfhelp/current/topic/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpm1/m1rhnvf.html

此警告类似于 Return with no value 中描述的警告。如果控制到达函数末尾并且没有遇到返回,则 GCC 假定返回没有返回值。然而,为此,该函数需要一个返回值。在函数末尾添加一个 return 语句,该语句返回合适的返回值,即使控制永远不会到达那里。

从原型来看,你似乎想返回一些东西。

http://publib.boulder.ibm.com/infocenter/tpfhelp/current/topic/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpm1/m1rhnvf.html

This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.

From the prototype it seems you want to return something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文