今天碰到一个题目,写bfs写炸了,发现好像数组越界,但是却没有报错,而是被边界检查拦截了
导致死循环了???
ps:所以写边界问题,首要的就是对边界进行检查,不检查越界之后会发生什么结果,你也不清楚!!!
下面是一个示例,展示如何使用负下标
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long int
const int maxLine=500+10;
bool checkMap[maxLine][maxLine];
int arr[maxLine];
signed main(){
for(int i=0;i<maxLine;i++){
arr[i]=i;
}
int *p=arr+maxLine/2;
cout<<p[-1];
return 0;
}
输出答案是254(即255的前一位)
你发现了吗?指针下标索引,实质上就是对地址的偏移索引,使用下标就是使用对应的地址
地址可以怎么玩,下标也可以怎么玩!