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建立二维数组在临时访问越界处不会报错,而数组临时访问会报错呢?

你这个数组越界,应该是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)); 应该就可以了

用最简单的说法是,定义数组,中括号[]中的内容,只能是常量,不能是变量
我举一个不是很恰当的例子:
假如你使用变量定义了一个数组arr,int index = 10; int arr[index];
index是可以改变的,例如 index = 20;
如果后面你再使用index去作为数组的长度判断或者用来for循环就导致越界了。for (int i = 0; i < index; i++) { printf("%d\n", arr[i]); } // 会导致数组越界
我这个例子不恰当的哈,只是列举给你说一下,不管是C还是C++,都不允许使用变量作为长度去定义数组!
根据你的代码,你可以用常量:
例如:

// 使用const进行修饰,使其变为常量
const int rows = matrix.size();  
const int cols = matrix[0].size();
 
int n = rows * cols;
 
int visit[rows][cols];

这样写应该是可以的!

C++语言不支持C99的变长数组,所以你不能这样定义二维数组int visit[rows][cols];,你可以动态分配二维数组。

你把C的语法和C++的语法搞混了
C++不支持变长数组
但可以随时定义变量,并不要求所有变量在一开始定义
你完全可以等row和col赋值之后再用他们来定义数组

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632