cw_yuk7 2022-02-21 00:08 采纳率: 100%
浏览 208
已结题

C++:关于vector和数组索引越界的问题

LeetCode. 54 螺旋矩阵,在判断方向是否需要调转时,为什么定义int类型visit数组在判断条件visit[nextrow][nextcol]处在提交时会产生数组越界的异常,而使用vector定义二维数组visit没有该问题?

报错程序:

 vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int rows = matrix.size();
        int cols = matrix[0].size();

        int n = rows * cols;

        int visit[rows][cols];

        int curderection = 0;
        int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

        int row = 0, col = 0;
        vector<int> res;
        for(int i = 0; i < n; i++){
            res.push_back(matrix[row][col]);
            visit[row][col] = 1;
            
            int nextrow = row + dir[curderection][0];
            int nextcol = col + dir[curderection][1];
            if(nextcol < 0 || nextrow < 0 || nextrow >= rows || nextcol >= cols || visit[nextrow][nextcol] ){
                curderection = (curderection + 1) % 4;
            }
            row += dir[curderection][0];
            col += dir[curderection][1];
        }
        return res;
    }

通过代码:

vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int rows = matrix.size();
        int cols = matrix[0].size();

        int n = rows * cols;

        vector<vector<bool>> visit(rows, vector<bool>(cols)); // 用数组会报越界错误

        int curderection = 0;
        int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

        int row = 0, col = 0;
        vector<int> res;
        for(int i = 0; i < n; i++){
            res.push_back(matrix[row][col]);
            visit[row][col] = 1;
            
            int nextrow = row + dir[curderection][0];
            int nextcol = col + dir[curderection][1];
            if(nextcol < 0 || nextrow < 0 || nextrow >= rows || nextcol >= cols || visit[nextrow][nextcol] ){
                curderection = (curderection + 1) % 4;
            }
            row += dir[curderection][0];
            col += dir[curderection][1];
        }
        return res;
    }

运行结果及报错内容

报错内容:
Line 1034: Char 34: runtime error: addition of unsigned offset to 0x6020000000f0 overflowed to 0x6020000000ec (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34

为什么使用vector建立二维数组在临时访问越界处不会报错,而数组临时访问会报错呢?

  • 写回答

5条回答 默认 最新

  • 爱在凌晨 2022-02-21 09:34
    关注

    你这个数组越界,应该是visit没有初始化造成的问题

    vector<vector<bool>> visit;// vector 底层自动给初始化为0了,所以用visit[nextrow][nextcol],判断有没有访问过,是正确的
    if(nextcol < 0 || nextrow < 0 || nextrow >= rows || nextcol >= cols || visit[nextrow][nextcol] ){
      curderection = (curderection + 1) % 4;
    }
    row += dir[curderection][0];
    col += dir[curderection][1];
    
    但是int visit[rows][cols]; // 直接定义数组,里面是没有被初始化数据的,这时候 visit[nextrow][nextcol] 判断就不对了
    如果某个位置上的visit初始值不为0,执行了
    if(nextcol < 0 || nextrow < 0 || nextrow >= rows || nextcol >= cols || visit[nextrow][nextcol] ){
      curderection = (curderection + 1) % 4;
    }
    row += dir[curderection][0];
    col += dir[curderection][1];
    
    row,col 可能就越界了, matrix[row][col] 就 报错了
    可以加个memset(visit,0,sizeof(vit)); 应该就可以了
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月3日
  • 已采纳回答 2月23日
  • 创建了问题 2月21日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效