简体   繁体   中英

Unable to resolve memory read and segmentation fault error in C++

I am coding a solution program for leetcode. I have encountered memory read errors and segmentation fault errors which I am unable to resolve. I have tried tinkering with the code to try and remove the errors.
For example: in the numIslands function's last lines, I had tried declaring coordinates as an array rather than a pointer but the results did not change.
*(last + 1) = *(coordinates + 1); does not seem to work at all in the numIslands function. I am totally blank about what to do next.

#include <iostream>
#include <string>
#include <vector>
#include <map>

namespace p200 {
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    using std::vector;
    using std::map;
}

class Solution {
private:
    int count_islands = 0;
    
public:
    int *last;
    int numIslands(vector<vector<char>>& grid) {
        if (grid.size() == 0) return count_islands;
        if (grid.size() == 1 && grid[0].size() == 1) return grid[0][0] - 48;
        vector<vector<int>> grid_copy;
        vector<int> in;
        for (size_t index = 0; index < grid.size(); ++index) {
            for (size_t index0 = 0; index0 < grid[index].size(); ++index0) {
                (grid[index][index0] == '0')
                    ? in.push_back(0)
                    : in.push_back(INT_MAX);
            }
            grid_copy.push_back(in);
            in.clear();
        }
        int * coordinates = new int[2];
        *coordinates = 0;
        *(coordinates + 1) = 0;
        last = new int[2];
        *last = *coordinates;
        *(last + 1) = *(coordinates + 1);
        find_island(grid_copy, coordinates);
        return count_islands;
    }

    void find_island(vector<vector<int>> & arg_grid, int* arg_coordinates) {
        if (arg_grid[*arg_coordinates][*(arg_coordinates + 1)] == INT_MAX) {
            *last = *arg_coordinates;
            *(last + 1) = *(arg_coordinates + 1);
            map_island(arg_grid, arg_coordinates, 1);
        } else {
            if (*(arg_coordinates + 1) < arg_grid[*arg_coordinates].size()) {
                *(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
                find_island(arg_grid, arg_coordinates);
            }
            if (*(arg_coordinates + 1) == arg_grid[*arg_coordinates].size()) {
                if (*arg_coordinates == arg_grid.size()) return;
                *arg_coordinates = *arg_coordinates + 1;
                *(arg_coordinates + 1) = 0;
                find_island(arg_grid, arg_coordinates);
            }
        }   
    }

    void map_island(vector<vector<int>> & arg_grid, int* arg_coordinates, int arg_new) {
        arg_grid[*arg_coordinates][*(arg_coordinates + 1)] = count_islands + (arg_new * 1);
        if (arg_new) count_islands = count_islands + 1;
        if (*arg_coordinates != 0 && *arg_coordinates - 1 == INT_MAX) {
            *arg_coordinates = *arg_coordinates - 1;
            map_island(arg_grid, arg_coordinates, 0);
            *arg_coordinates = *arg_coordinates + 1;
        }
        if ((*arg_coordinates < arg_grid.size() - 1)
             && *arg_coordinates + 1 == INT_MAX) {
            *arg_coordinates = *arg_coordinates + 1;
            map_island(arg_grid, arg_coordinates, 0);
            *arg_coordinates = *arg_coordinates - 1;
        }
        if (*(arg_coordinates + 1) != 0 && *(arg_coordinates + 1) - 1 == INT_MAX) {
            *(arg_coordinates + 1) = *(arg_coordinates + 1) - 1;
            map_island(arg_grid, arg_coordinates, 0);
            *(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
        }
        if (*(arg_coordinates + 1) < arg_grid[*arg_coordinates].size() - 1
            && *(arg_coordinates + 1) + 1 == INT_MAX) {
            *(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
            map_island(arg_grid, arg_coordinates, 0);
            *(arg_coordinates + 1) = *(arg_coordinates + 1) - 1;
        }
        find_island(arg_grid, last);
    }
};


int main() {
    vector<vector<char>> input;
    input.push_back(vector<char>({'1','1','1','1','0'}));
    input.push_back(vector<char>({'1','1','0','1','0'}));
    input.push_back(vector<char>({'1','1','0','0','0'}));
    input.push_back(vector<char>({'0','0','0','0','0'}));
    Solution solver;
    cout << "Number of Island: " << solver.numIslands(input) << endl;
    return 0;
}

I have ran it through gdb. After a bit of digging, the error is this:

    void find_island(vector<vector<int>> & arg_grid, int* arg_coordinates) {
        if (arg_grid[*arg_coordinates][*(arg_coordinates + 1)] == INT_MAX) {
            *last = *arg_coordinates;
            *(last + 1) = *(arg_coordinates + 1);
            map_island(arg_grid, arg_coordinates, 1);
        } else {
            if (*(arg_coordinates + 1) < arg_grid[*arg_coordinates].size()) {
                *(arg_coordinates + 1) = *(arg_coordinates + 1) + 1;
                find_island(arg_grid, arg_coordinates);
            }
            if (*(arg_coordinates + 1) == arg_grid[*arg_coordinates].size()) {
                if (*arg_coordinates == arg_grid.size()) return;
                *arg_coordinates = *arg_coordinates + 1;
                *(arg_coordinates + 1) = 0;
                find_island(arg_grid, arg_coordinates);
            }
        }
    }

In the second if , you do a bounds check and then increment. You should first increment then to the bounds check, like this:

*arg_coordinates = *arg_coordinates + 1;
if (*arg_coordinates == arg_grid.size()) return;

Also please use vector.at() instead of operator[] . It produces more readable and easier to interpret debug messages. Also as other people pointed it out *(arg_coordinates + 1) = arg_coordinates[1] , which would make for a more readable code.

For other similar issues I recommend a debugger.

Edit: I ran the code and realised that there is some other bug within it. One of the islands is counted twice, so it gives the wrong result, but that is not technically the issue at hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM