【发布时间】:2021-04-02 15:33:44
【问题描述】:
我在 win 10 上使用 clang (10.0) 进行实验,使用以下 sn-p:
#include <iostream>
using namespace std;
int main()
{
const int N = 10;
int ii;
cout << "Enter value for ii: ";
cin >> ii;
int a[N+ii];
for (int i = 0; i < N+ii; ++i) {
a[i] = 0;
}
for (int i = 0; i < N+ii; ++i) {
cout << "a[ " << i << "] = " << a[i] << endl;
}
return 0;
}
我不确定为什么没有编译错误(数组a的大小在编译时是未知的)????
PS:
- 两个
for循环都是为了触发分段错误。 - 编译命令:
clang.exe exp.cpp - 我尝试使用较大的
ii值运行代码,但它似乎工作正常(ii = 10000、100000、1000000)
【问题讨论】:
标签: arrays memory-leaks clang dynamic-memory-allocation