我有一个功能如下:
#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
typedef struct _coordinate{
int posx;
int posy;
_coordinate(int x = 0,int y = 0){posx = x; posy = y;};
}Coordinate;
int find_way(int x, int y, int food_x, int food_y, vector <string>& grid, vector<Coordinate>& co, vector<Coordinate>& cur, i
nt& min){
if(cur.empty()){
if(!co.empty()){
return 1;
}
else{
return 0;
}
}
Coordinate node = cur.back();
int nx = 0;
int ny = 0;
if((node.posx == food_x) && (node.posy == food_y)){
if(cur.size() < min){
co.clear();
for(int i = 0; i < cur.size(); i++){
co.push_back(cur[i]);
cout<<cur[i].posx<<" "<<cur[i].posy<<endl;
}
min = co.size();
}
cur.pop_back();
return find_way(x, y, food_x, food_y, grid, co, cur, min);
}
//DOWN
if((node.posx < x-1) && (grid[node.posx+1].at(node.posy) != '%')){
nx = node.posx+1;
ny = node.posy;
grid[nx][ny] = '%';
cur.push_back(Coordinate(nx,ny));
cout<<"DOWN "<<nx<<" "<<ny<<endl;
cout<<"grid.size()="<<grid.size()<<", grid[nx].length()"<<grid[nx].length()<<endl;
find_way(x, y, food_x, food_y, grid, co, cur, min);
cur.pop_back();
grid[nx][ny] = '-';
}
//RIGHT
if((node.posy < y-1) && (grid[node.posx].at(node.posy+1) != '%')){
nx = node.posx;
ny = node.posy+1;
grid[nx][ny] = '%';
cur.push_back(Coordinate(nx,ny));
cout<<"RIGHT "<<nx<<" "<<ny<<endl;
cout<<"grid.size()="<<grid.size()<<", grid[nx].length()"<<grid[nx].length()<<endl;
find_way(x, y, food_x, food_y, grid, co, cur, min);
cur.pop_back();
grid[nx][ny] = '-';
}
//LEFT
if((node.posy > 0) && (grid[node.posx].at(node.posy-1) != '%')){
nx = node.posx;
ny = node.posy-1;
grid[nx][ny] = '%';
cur.push_back(Coordinate(nx,ny));
cout<<"LEFT "<<nx<<" "<<ny<<endl;
cout<<"grid.size()="<<grid.size()<<", grid[nx].length()"<<grid[nx].length()<<endl;
find_way(x, y, food_x, food_y, grid, co, cur, min);
cur.pop_back();
grid[nx][ny] = '-';
}
//UP
if((node.posx > 0) && (grid[node.posx-1].at(node.posy) != '%')){
nx = node.posx-1;
ny = node.posy;
grid[nx][ny] = '%';
cur.push_back(Coordinate(nx,ny));
cout<<"UP "<<nx<<" "<<ny<<endl;
cout<<"grid.size()="<<grid.size()<<", grid[nx].length()"<<grid[nx].length()<<endl;
find_way(x, y, food_x, food_y, grid, co, cur, min);
cur.pop_back();
grid[nx][ny] = '-';
}
else{
cur.pop_back();
cout<<"pop_back"<<endl;
//find_way(x, y, food_x, food_y, grid, co, cur, min);
}
}
当我运行这个程序时,发生了崩溃,核心转储如下:
(gdb) bt
#0 0x000000302af2e2ed in raise () from /lib64/tls/libc.so.6
#1 0x000000302af2fa3e in abort () from /lib64/tls/libc.so.6
#2 0x000000302d3b1138 in __gnu_cxx::__verbose_terminate_handler () from /usr/lib64/libstdc++.so.6
#3 0x000000302d3af166 in __cxa_call_unexpected () from /usr/lib64/libstdc++.so.6
#4 0x000000302d3af193 in std::terminate () from /usr/lib64/libstdc++.so.6
#5 0x000000302d3af293 in __cxa_throw () from /usr/lib64/libstdc++.so.6
#6 0x000000302d3af61d in operator new () from /usr/lib64/libstdc++.so.6
#7 0x000000302d3901de in std::string::_Rep::_S_create () from /usr/lib64/libstdc++.so.6
#8 0x000000302d3908db in std::string::_M_mutate () from /usr/lib64/libstdc++.so.6
#9 0x000000302d391e31 in std::string::_M_leak_hard () from /usr/lib64/libstdc++.so.6
#10 0x000000302d391eb8 in std::string::at () from /usr/lib64/libstdc++.so.6
#11 0x00000000004015b4 in find_way (x=7, y=20, food_x=5, food_y=1, grid=@0x7fbffff8b0, co=@0x7fbffff820, cur=@0x7fbffff7f0,
min=@0x7fbffff7ec) at PacMan-DFS.cpp:74
#12 0x0000000000401562 in find_way (x=7, y=20, food_x=5, food_y=1, grid=@0x7fbffff8b0, co=@0x7fbffff820, cur=@0x7fbffff7f0,
min=@0x7fbffff7ec) at PacMan-DFS.cpp:69
#13 0x0000000000401562 in find_way (x=7, y=20, food_x=5, food_y=1, grid=@0x7fbffff8b0, co=@0x7fbffff820, cur=@0x7fbffff7f0,
min=@0x7fbffff7ec) at PacMan-DFS.cpp:69
想知道为什么在使用函数“string.at()”的时候会调用“operator new()”,貌似不需要申请内存块,为什么会发生crash,谢谢!