我有一个接受结构(指针)的函数。在此函数中,我遍历全局结构的队列的末尾,并将新结构添加到队列的末尾。完成此操作后,出于错误检查的目的,我需要将传入的结构(指针)设置为等于null而不影响全局队列。但是,当我将传入的结构设置为NULL时,会导致队列出现段错误。
编辑:我无法发布完整的代码,因为它对类敏感,但是我可以发布示例。
示例:
//There is code above this parsing the passedInJob in various ways, adding it to the queue if the final step
//This block of code just adds to queue, after this all references to the newly added job cause a segfault
//get the current head of the queue and put it in currJob
struct job * currJob = queue->head;
//Move to the end of the queue
while(currJob->nextJob != NULL)
//move to the next job, when we hit NULL we're at the end
currJob = currJob->nextJob
//Set the end of the queue to be the passed in job
currJob->next_job = passedInJob;
//We set passedInJob = NULL because at the end we check if the job still exists to signify there was an error adding the job
//This is causing the error, I believe setting this to NULL is setting the struct in the queue to NULL
passedInJob = NULL;
//Since there is code parsing above, we use this to ensure the job was added
if(passedInJob)
//handle error