c++ - A core about string -
i have function follow:
#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, 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 = 0; < 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); } }
when run program, crash occured, , coredump follow:
(gdb) bt #0 0x000000302af2e2ed in raise () /lib64/tls/libc.so.6 #1 0x000000302af2fa3e in abort () /lib64/tls/libc.so.6 #2 0x000000302d3b1138 in __gnu_cxx::__verbose_terminate_handler () /usr/lib64/libstdc++.so.6 #3 0x000000302d3af166 in __cxa_call_unexpected () /usr/lib64/libstdc++.so.6 #4 0x000000302d3af193 in std::terminate () /usr/lib64/libstdc++.so.6 #5 0x000000302d3af293 in __cxa_throw () /usr/lib64/libstdc++.so.6 #6 0x000000302d3af61d in operator new () /usr/lib64/libstdc++.so.6 #7 0x000000302d3901de in std::string::_rep::_s_create () /usr/lib64/libstdc++.so.6 #8 0x000000302d3908db in std::string::_m_mutate () /usr/lib64/libstdc++.so.6 #9 0x000000302d391e31 in std::string::_m_leak_hard () /usr/lib64/libstdc++.so.6 #10 0x000000302d391eb8 in std::string::at () /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) @ 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) @ 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) @ pacman-dfs.cpp:69
i want know why there call "operator new ()" when use function "string.at()", seems there no need apply block of memory, , why crash happened, thanks!
std::string.at() might call operator new if using reference counted implementation of std::string.
in reference counted implementation 2 strings might share same underlying character array. because at()
on non-const string returns non-const reference in such circumstances implementation must allocate new character array because otherwise reference returned at()
used modify both strings.