【问题标题】:c, segmentation faultc、分段错误
【发布时间】:2011-08-10 03:11:35
【问题描述】:

谁能告诉我为什么我这里有分段错误?

   void *dispatcher_threadloop(void * queue){

//thread loop of the dispatch thread- pass the tast to one of worker thread
dispatch_queue_t *dQueue;

printf("message-boss1");
dQueue = (dispatch_queue_t *)queue;
if (dQueue->HEAD!=NULL){
    for(;;){
        sem_wait(dQueue->queue_thread_semaphore);
        //TODO
    }
}

printf("message-boss2");

}

【问题讨论】:

  • 你试过使用调试器吗?

标签: c if-statement segmentation-fault


【解决方案1】:

queue 可能为 0 或指向内存中的某个无效位置。

如果您需要更多帮助,请参阅http://sscce.org/

【讨论】:

    【解决方案2】:

    如果您不想检查 dQueue 与 null,或者如果 dQueue 永远不会为 null,请尝试断言

    dispatch_queue_t *dQueue = queue;
    
    assert(dQueue!=NULL)
    

    另一方面,您可能应该发布queue 变量的来源。

    【讨论】:

    • 那么我肯定会选择断言。
    • 什么不起作用?断言?你期待什么,发生了什么?
    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多