runtime error: reference binding to misaligned address 0xbebebebebebebec6 for type 'int', which requires 4 byte alignment (stl_vector.h)

runtime error: reference binding to misaligned address 0xbebebebebebebec6 for type 'int', which requires 4 byte alignment (stl_vector.h)

我正在写代码来解决this problem on leetcode
我解决这个问题的策略是:

class Solution {
public:
    void psUtil(vector<vector<int> >&mat, int x, int y, int m, int n, int &isP, int &isA, vector<vector<int> >&vis, vector<vector<int> >&ans)
    {
        //check dstinations
        if(x == 0 || y == 0)
        {
            isP = 1;
        }
        if(x == m || y == n)
        {
            isA = 1;
        }

        vector<int> cell(2);
        cell[0] = x;
        cell[1] = y;

        // check both dst rched
        if(isA && isP)
        {
            // append to ans
            ans.push_back(cell);
            return;
        }
        // mark vis
        vis.push_back(cell);

        int X[] = {-1, 0, 1, 0};
        int Y[] = {0, 1, 0, -1};
        int x1, y1;

        // check feasible neighbours
        for(int i = 0; i < 4; ++i)
        {
            x1 = x + X[i];
            y1 = y + Y[i];
            if(x1 < 0 || y1 < 0) continue;

            if(mat[x1][y1] <= mat[x][y])
            { 
                vector<vector<int> > :: iterator it;
                vector<int> cell1(2);
                cell1[0] = x1;
                cell1[1] = y1;
                it = find(vis.begin(), vis.end(), cell1);
                if(it == vis.end());
                else continue;
                psUtil(mat, x1, y1, m, n, isP, isA, vis, ans);
                if(isA && isP) return; 
            }
        }
    }
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) 
    {
        // find dimensions
        int m = matrix.size(); // rows
        int n = matrix[0].size(); // cols
        vector<vector<int> >ans;
        // flags if rched destinations
        int isP, isA;
        isP = isA = 0;
        // iterate for all indices
        for(int x = 0; x < m; ++x)
        {
            for(int y = 0; y < n; ++y)
            {
                // visited nested vector
                vector<vector<int> >vis; 
                psUtil(matrix, x, y, m, n, isP, isA, vis, ans);
                isP = isA = 0;    
            }
        }
        return ans;     
    }
};

我在 运行 上的错误是

Runtime Error Message:
Line 924: Char 9: runtime error: reference binding to misaligned address 0xbebebebebebebec6 for type 'int', which requires 4 byte alignment (stl_vector.h)
Last executed input:
[[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]

为什么我会收到这条消息,我该如何解决?

你的方法很好,但也许我们可以在实现上改进一下。这是采用类似 DFS 方法的公认解决方案。

class Solution {
public:
    int direction_row[4] = {0, 1, -1, 0};
    int direction_col[4] = {1, 0, 0, -1};

    void depth_first_search(vector<vector<int>> &grid, vector<vector<bool>> &visited, int row, int col, int height) {
        if (row < 0 || row > grid.size() - 1 || col < 0 || col > grid[0].size() - 1 || visited[row][col])
            return;

        if (grid[row][col] < height)
            return;

        visited[row][col] = true;

        for (int iter = 0; iter < 4; iter++)
            depth_first_search(grid, visited, row + direction_row[iter], col + direction_col[iter], grid[row][col]);
    }

    vector<vector<int>> pacificAtlantic(vector<vector<int>> &grid) {
        vector<vector<int>> water_flows;
        int row_length = grid.size();

        if (!row_length)
            return water_flows;

        int col_length = grid[0].size();

        vector<vector<bool>> pacific(row_length, vector<bool>(col_length, false));
        vector<vector<bool>> atlantic(row_length, vector<bool>(col_length, false));

        for (int row = 0; row < row_length; row++) {
            depth_first_search(grid, pacific, row, 0, INT_MIN);
            depth_first_search(grid, atlantic, row, col_length - 1, INT_MIN);
        }

        for (int col = 0; col < col_length; col++) {
            depth_first_search(grid, pacific, 0, col, INT_MIN);
            depth_first_search(grid, atlantic, row_length - 1, col, INT_MIN);
        }

        for (int row = 0; row < row_length; row++)
            for (int col = 0; col < col_length; col++)
                if (pacific[row][col] && atlantic[row][col]) {
                    water_flows.push_back({row, col});
                }

        return water_flows;
    }
};

我也不确定这是否是解决太平洋大西洋水流问题的最有效算法。您可以查看讨论区。


参考资料

我发现了我的错误!这是因为缺少对新计算的坐标的边界检查,并且在 psUtil 的开头对坐标进行了不正确的边界检查。

而不是这个:

if(x == m || y == n)
.
.
.
if(x1 < 0 || y1 < 0) continue; 

应该是这样的:

if(x == m-1 || y == n-1)
.
.
.
if(x1 < 0 || y1 < 0 || x1 >= m || y1 >= n) continue;