2013-04-25 48 views
-5

我有一個函數如下:約串核心

#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, [email protected], [email protected], [email protected], 
     [email protected]) at PacMan-DFS.cpp:74 
    #12 0x0000000000401562 in find_way (x=7, y=20, food_x=5, food_y=1, [email protected], [email protected], [email protected], 
     [email protected]) at PacMan-DFS.cpp:69 
    #13 0x0000000000401562 in find_way (x=7, y=20, food_x=5, food_y=1, [email protected], [email protected], [email protected], 
     [email protected]) at PacMan-DFS.cpp:69 

我想知道爲什麼會出現當我使用函數「string.at()」時,調用「operator new()」,似乎沒有必要申請一塊內存,並且爲什麼會發生崩潰,謝謝!

+0

這是,在我看來,至少,太多的代碼。您應該將其降低到可以再現錯誤的最小量。 – unwind 2013-04-25 08:28:28

+0

UP,DOWN,LEFT,RIGHT代碼相當多餘。將公共部分移到他們自己的功能。 – MSalters 2013-04-25 08:48:32

回答

3

如果您使用引用計數執行std :: string,std :: string.at()可能會調用operator new。

在引用計數實現中,兩個字符串可能共享相同的基礎字符數組。但是因爲在非常量字符串上的at()在這種情況下返回一個非const引用,所以實現必須分配一個新的字符數組,因爲否則從at()返回的引用可能用於修改這兩個字符串。

+0

但是當我使用[]而不是()時,崩潰也發生了,並且生成了相同的核心庫。 – Spirit 2013-04-25 08:39:10

+0

同樣的道理。 'at()'和'operator []'之間的區別是範圍檢查,'.at'只會在檢查範圍通過時分配內存。 – MSalters 2013-04-25 08:49:42