c++ - How to memset a 2D arrray? -
i want memset 2d array 0. here's code.. giving me seg fault;
bool **visited=new bool*[m]; for(int i=0;i<m;++i) visited[i] = new bool[m];
i have tried memset(visited, 0, sizeof(visited[0][0]) * m * m);
, memset(visited, 0, sizeof visited);
, nonw of works , gives me segfault. how do that?
your array not contiguous since not multi-dimensional array. it's array of arrays, known jagged array.
so, rows can, , will, disjoint. hence you'll need call memset on each row.
bool **visited=new bool*[m]; for(int i=0;i<m;++i) { visited[i] = new bool[m]; memset(visited[i], 0, sizeof(visited[i][0]) * m); }
although, can't refrain pointing out should using c++ features rather writing appears c use of new operator.
Comments
Post a Comment