当我使用以下代码编译器(-Wall -O2
)进行编译时,g++
会发出警告:数组下标超出数组边界...
#include <iostream>
#include <algorithm>
using namespace std;
int a[10];
int n;
int main(){
sort(a, a+n);
return 0;
}
但是这段代码编译时没有任何警告:
#include <iostream>
#include <algorithm>
using namespace std;
int a[100];
int n;
int main(){
sort(a, a+n);
return 0;
}
为什么会发生这种情况?
0
当然了。全局原语总是被初始化为零。 - orlp