-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, 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,谢谢!

4

1 回答 1

3

如果您使用 std::string 的引用计数实现,std::string.at() 可能会调用 operator new。

在引用计数的实现中,两个字符串可能共享相同的底层字符数组。但是因为at()在这种情况下非常量字符串返回非常量引用,所以实现必须分配一个新的字符数组,否则返回的引用at()可以用于修改两个字符串。

于 2013-04-25T08:29:44.717 回答