Click here to Skip to main content
15,881,719 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code for problem Number of Islands in Leetcode is giving Runtime error.
class Solution {
public:
    vector<pair<int,int>> movements={{1,0},{-1,0},{0,1},{0,-1}};
    bool isValid(int i,int j,int n,int m){
        if((i>=n && i<n) && (j>=m && j<m)){
            return 1;
        }
        return 0;
    }
    int numIslands(vector<vector<char>>& grid) {
        int count=0;
        queue<pair<int,int>> q;
        int row=grid.size();
        int col=grid[0].size();
        vector<vector<bool>> visited;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                visited[i][j]=0;
            }
        }
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(grid[i][j]=='1'){
                    q.push(make_pair(i,j));
                }
            }
        }
        while(!q.empty()){
            auto it=q.front();
            q.pop();
            int x=it.first;
            int y=it.second;
            visited[x][y]=1;
            count++;
            for(auto movement:movements){
                int childx=movement.first+x;
                int childy=movement.second+y;
                if(isValid(childx,childy,row,col) && !visited[childx][childy]){
                    if(grid[childx][childy]=='1'){
                    visited[childx][childy]=1;
                    q.push(make_pair(childx,childy));
                    }        
                }
            }
        }
        return count;
    }
};


What I have tried:

I tried looking in Discuss section of Leetcode other people BFS code is just as same as mine.
The error Leetcode debugger throwing is:
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<bool, std::allocator<bool>>' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
Posted
Updated 23-Jul-22 5:59am
v2
Comments
Peter_in_2780 22-Jul-22 22:07pm    
Time to learn how to use the debugger. We can't help if we don't even know what error it's throwing up.
Animesh _1SG20IS008 23-Jul-22 0:09am    
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<bool, std::allocator<bool="">>' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
Patrice T 22-Jul-22 23:06pm    
And you plan to tell us which error message and position ?
Or we have to guess ?
Animesh _1SG20IS008 23-Jul-22 0:09am    
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<bool, std::allocator<bool="">>' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
Patrice T 23-Jul-22 0:11am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

I think this is incorrect :
if((i>=n && i<n) && (j>=m && j<m))
so IsValid will always return false.

I would guess that it should be :
if( ( i >= 0 && i < n ) && ( j >= 0 && j < m ) )
 
Share this answer
 
Comments
Animesh _1SG20IS008 23-Jul-22 0:16am    
Thanks Rick for the help but still its giving the same error.
I see the real problem. It's this section :
C++
vector<vector<bool>> visited;
for(int i=0;i<row;i++){
    for(int j=0;j<col;j++){
        visited[i][j]=0;
    }
}
You are accessing an uninitialized vector. Here's one way you could initialize it :
C++
using vbool = std::vector< bool >;
using vvbool = std::vector< vbool >;

vvbool visited;
for( int i = 0; i < row; ++i )
{
    vbool rowvec( col, 0 );
    visited.push_back( rowvec );
}
 
Share this answer
 
Comments
Animesh _1SG20IS008 23-Jul-22 7:59am    
Thanks it worked.
merano99 23-Jul-22 12:13pm    
Why is the creation of rowvec in the loop?
Rick York 23-Jul-22 12:37pm    
Because visited is a two dimensional vector - a vector of vectors. That means each row is a vector also so vectors must be pushed into the vector.
merano99 23-Jul-22 14:36pm    
The two dimensions are clear, but the vector's values ​​are probably copied every time, aren't they?
Just tested with VS. The end result is the same if you don't keep updating the vector rowvec in the loop. In your version, however, many new classes are created and destroyed.
C++
if((i>=n && i<n) && (j>=m && j<m)){

The value of i cannot be greater than or equal to n and less than n at the same time. The same goes for j and m.
 
Share this answer
 
Since the main program is missing, the program is difficult to test. If the solution is empty, the program will certainly raise exceptions here.
C++
int row = grid.size();
int col = grid[0].size();

This could easily be fixed by increasing the container grid to a minimum size. The same applies to the container visited.
C++
int row = grid.size();
if (row == 0)
  grid.resize(1);
int col = grid[0].size();

It is noticeable that the containers grid and visited always have to be the same size and maybe also belong in the solution.
There you could keep them consistent and always have access and size appropriate. The fields do not have to be transferred or created anew each time.
So I propose a member function InitGrid().
C++
class Solution {
private:
   vector<vector<char>> grid;
   vector<vector<bool>> visited;
public:
   vector<pair<int, int>> const movements = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
   bool InitGrid(int row, int col);
   bool isValid(int i, int j, int n, int m);
   int numIslands();
};

C++
bool Solution::InitGrid(int row, int col)
{
  ...
  return true;
}


For the isValid function, Rick's suggestion makes sense. Otherwise the program would be useless, but it wouldn't crash either.
Since the function declares a return value of type bool, it should also return true or false.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900