简体   繁体   中英

A segmentation fault?

I am currently working on this project for school, where I am implementing an algorithm to find a solution for the 'knight game' (it's about finding the shortest way from the top left corner of the board to the bottom right corner), but I've been getting this segmentation fault for three days now, I've checked every pointer I used and everything seems right. I implemented " searching algorithms, bfs and dfs and ucs, the first two ones work fine, but ucs gives me the segmentation fault, even though they use the same thing except a popBest function. Here are some pictures of the ucs and popBest function:

Item *popBest( list_t *list ) // and remove the best board from the list.
{
  assert(list);
  assert(list->numElements);

  int min_f;

  Item *item = list->first;
  Item *best;
  min_f = list->first->f;

  while (item) {
    if (item->f < min_f) {
      min_f = item->f;
      best = item;
    }
    item = item->next;
  }
  //item = onList(list, board);
  delList(list, best);

  return best;
}

void ucs(void)
{
    Item *cur_node, *child_p, *temp;

    while ( listCount(&openList_p) ) { /* While items are on the open list 
        printLt(openList_p );
        /* Get the first item on the open list*/
        cur_node = popBest(&openList_p);
        //printf("%d  %f\n", listCount(&openList_p), evaluateBoard( cur_node ));
        printBoard(cur_node);

        addFirst(&closedList_p, cur_node);

        if ( evaluateBoard(cur_node) == 0.0 ) {
            showSolution(cur_node);
            printf("\nParcours en largeur (bfs)\n" );
            return;
       }
       else {
            for (int i = 0; i < MAX_BOARD; i++) {
                child_p = getChildBoard( cur_node, i );

                if (child_p != NULL) {
                    child_p->f = cur_node->f+1;
                    temp = onList(&openList_p, child_p->board);
                    if (temp ==NULL) addLast( &openList_p, temp);
                    else if (temp != NULL && child_p->f < temp->f )
                    {
                        delList(&openList_p, temp);
                        addLast( &openList_p, temp);
                    }
                  }
                }
            }
        }

    return;
}

All the functions work fine for bfs and dfs, the only difference is the popBest function.

You do list->first->f without checking whether list->first is the null pointer.

The cause of your problem is probably that best is potentially uninitialised after the loop, and it will definitely be if the first element in the list is "best".

Here is a safer version.

Item *popBest( list_t *list )
{
  assert(list);
  assert(list->numElements);
  assert(list->first);

  // Assume that the first element is best.
  Item *best = list->first;
  int min_f = best->f;

  // Search from the second element (if it exists).
  Item* item = best->next;
  while (item) {
    if (item->f < min_f) {
      min_f = item->f;
      best = item;
    }
    item = item->next;
  }
  delList(list, best);
  return best;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM