cpp では、配列宣言を typename array[size];として使用できます。 または typename *array = new typename[size]; 配列の長さは 'size' で、要素は '0' から 'size -1' までのインデックスが付けられています。
だから私はそれをチェックするためにこの小さなコードを書きました
#include <iostream>
using namespace std;
int main()
{
//int *c; //for dynamic allocation
int n; //length of the array c
cin>>n; //getting the length
//c = new int[n]; //for dynamic allocation
int c[n]; //for static allocation
for(int i=0; i<n; i++) //getting the elements
cin>>c[i];
for(int i=0; i<n+10; i++) //showing the elements, I have add up 10
cout<<c[i]<<" "; //with size to access the memory I haven't
//allocated for
return 0;
}
で、結果はこんな感じ
2
1 2
1 2 2686612 1970422009 7081064 4199040 2686592 0 1 1970387429 1971087432 2686700
プログラムがクラッシュするのではなく、ガベージ値を与えるべきではありません。そして、両方の割り当て方法で同じ結果が得られます。検出が困難なバグが増えます。使用している環境やコンパイラなどに関連していますか?
Windows 8.1でTDM-GCC 4.8.1コンパイラを備えたコードブロックIDEを使用していました
前もって感謝します。