簡體   English   中英

遞歸回溯迷宮生成器C ++中的分段錯誤

[英]Segmentation fault in recursive backtracking maze generator C++

我正在嘗試使用遞歸回溯創建一個迷宮生成器,並遇到了一個我無法理解的問題。 由於某種原因,我的移動功能返回值“18446744073709551615”。 這(當然)會導致分段錯誤。 當我的移動函數只能將值增加或減少2時,為什么我的移動函數返回如此大的值?

bool maze::generate(size_t x, size_t y) {

//mark the position as visited
labyrinth.s[y][x] = true;

//print to see progress
//this->print();

//if the position is not out of bounds
if (x < 0 || x > labyrinth.MAXWIDTH - 1 || y < 0 || y > labyrinth.MAXHIGHT - 1) {
//if the position is the endpoint return true
if (labyrinth.v[y][x - 1] == 'W' || labyrinth.v[y][x + 1] == 'W' || labyrinth.v[y - 1][x] == 'W' || labyrinth.v[y + 1][x] == 'W') {
  return true;
 }
}

//pick a random direction
do {
  d = size_t(rand() % 4);
} while(!this->pos_test(x, y, d));
std::cout << x << ' ' << y << std::endl;

if (d == UP) {
y = move(x, y, UP);
}
else if (d == DOWN) {
y = move(x, y, DOWN);
}
else if (d == RIGHT) {
x = move(x, y, RIGHT);
}
else if (d == LEFT) {
x = move(x, y, LEFT);
}
else{
}
std::cout << x << ' ' << y << std::endl;
//recursively generate the maze
if (this->generate(x, y)) {
  return true;
}
}

void maze::initialize(size_t x, size_t y) {
   //set the maxhight and the maxwidth to y and x
   labyrinth.MAXHIGHT = y;
   labyrinth.MAXWIDTH = x;
   //set all elements in the vector to #
   for (size_t i = 0; i < labyrinth.MAXHIGHT; i++) {
     std::vector<char> temp;
     for (size_t j = 0; j < labyrinth.MAXWIDTH; j++) {
       temp.push_back(labyrinth.wall);
     }
     labyrinth.v.push_back(temp);
   }
   for (size_t i = 0; i < labyrinth.MAXHIGHT; i++) {
     for (size_t j = 0; j < labyrinth.MAXWIDTH; j++) {
       if (j % 2 == 1 && i % 2 == 1 && j != labyrinth.MAXWIDTH - 1 && j != 0 && i != labyrinth.MAXHIGHT - 1 && i != 0) {
         labyrinth.v[j][i] = labyrinth.path;
       }
     }
   }
     //set all posistions to unvisited
     for (size_t i = 0; i < labyrinth.MAXHIGHT; i++) {
       std::vector<bool> temp2;
       for (size_t j = 0; j < labyrinth.MAXWIDTH; j++) {
         temp2.push_back(false);
       }
       labyrinth.s.push_back(temp2);
    }
     //setup the start point
     labyrinth.v[0][1] = 'S';
     //setup the endpoint
     labyrinth.v[labyrinth.MAXHIGHT - 2][labyrinth.MAXWIDTH - 1] = 'W';

  }

  //if a position has been visited or if not possible to go to return true
 bool maze::pos_test(size_t x, size_t y, size_t d) const {
    //if the position is out of bounds return false
    if (x < 0 || y < 0 || x > labyrinth.MAXWIDTH - 1 || y > labyrinth.MAXHIGHT - 1) {
       return true;
   }
    else if (x == 1 && d == LEFT) {
      return true;
   }
    else if (y == 1 && d == UP) {
      return true;
   }
   else if (x == labyrinth.MAXWIDTH - 1 && d == RIGHT) {
     return true;
   }
   else if (y == labyrinth.MAXHIGHT - 1 && d == DOWN) {
     return true;
   }
   else if (d == UP) {
     return labyrinth.s[y - 2][x];
   }
   else if (d == DOWN) {
     return labyrinth.s[y + 2][x];
   }
   else if (d == RIGHT) {
     return labyrinth.s[y][x + 2];
   }
   else if (d == LEFT) {
     return labyrinth.s[y][x - 2];
   }
   else  {
     return true;
   }
 }


size_t maze::move(size_t x, size_t y, size_t d) {
//if the position is out of bounds return without modifying
if (x < 0 || x > labyrinth.MAXWIDTH - 1) {
  return x;
}
else if (y < 0 || y > labyrinth.MAXHIGHT - 1) {
  return y;
}
else if (d == UP) {
  labyrinth.v[y - 1][x] = labyrinth.path;
  return y =  y - 2;
}
else  if (d == DOWN) {
  labyrinth.v[y + 1][x] = labyrinth.path;
  return y = y + 2;
}
else if (d == RIGHT) {
  labyrinth.v[y][x + 1] = labyrinth.path;
  return x = x + 2;
}
else if (d == LEFT) {
  labyrinth.v[y][x - 1] = labyrinth.path;
  return x = x - 2;
}
else  {
}
}

您正在使用無符號的64位返回類型size_t進行下溢。

你正在檢查xy是否低於零,但這還不夠,因為0和1仍然太低,因為你減去2!

你得到的數字是十六進制的0xFFFFFFFFFFFFFFFF。 這是無符號64位整數的最高可能值。

它來自計算1 - 2 是的,這應該是-1 ,但是因為你的移動函數不返回有符號數而是無符號數(檢查size_t上的文檔),它不能是負數! 相反,它包含盡可能多的數字。

你可以想象這就像你得到的那樣... 99999999999當你試圖在紙上計算1 - 2時忽略“你不能從較小的紙上減去更高的數字”規則。

作為旁注:我認為負面結果無論如何都是不受歡迎的,因為實際上你的巨大數字,一旦被添加到指針,將反過來回到正面,所以基本上它將工作相同是在你的情況下真實-1和分段錯誤來自於緩沖區開始之前訪問某些內容,而不是遠遠超出它,但它歸結為同樣的事情。

除此之外,沒有必要return y = y - 2等。 只需return y - 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM