2016-12-14 9 views
1
#include<iostream> 
using namespace std; 
void arrayin(int x[], int n); 
void arrayout(int x[], int n); 
main() 
{ 
    int n, x[n]; 
    cout << "Please enter the number of elements in the array: " << endl; 
    cin >> n; 
    cout << "Please enter the elements: " << endl; 
    arrayin(x,n); 
    cout << "Array is of " << n << " elements."<< endl; 
    cout << "Elements are as follow :" << endl; 
    arrayout(x,n); 
} 
void arrayin(int x[],int n) 
{ 
    for (int i = 0; i < n; i ++) 
    { 
     cin >> x[i]; 
    } 
} 
void arrayout(int x[], int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     cout << x[i] << "\t"; 
    } 
} 

プログラミングが初めてです。 n> 8がクラッシュした場合、8つ以上の要素がクラッシュしますが、nの場合は<が正常に動作します。 なぜか分かりません!8個以上の要素で動的配列がクラッシュする

+3

'int型N '、' X [N]; '--->未定義の動作 – LPs

+0

私は「ドンC++はVLAをサポートしていますか?私はそれがCのものだと思ったが、私は間違っているかもしれない。 –

+2

C++としてコンパイルしていますか? (あなたが 'n 'を知る前に' x [n] 'を宣言してから本当に重要なことではありません)。可変長配列は 'C'のものです。C++では' vector'を使います。 – doctorlove

答えて

0

nを入力した後で配列を宣言します。

#include<iostream> 
using namespace std; 
void arrayin(int x[], int n); 
void arrayout(int x[], int n); 
main() 
{ 
    int n; 
    cout << "Please enter the number of elements in the array: " << endl; 
    cin >> n; 
    int x[n]; 
    cout << "Please enter the elements: " << endl; 
    arrayin(x,n); 
    cout << "Array is of " << n << " elements."<< endl; 
    cout << "Elements are as follow :" << endl; 
    arrayout(x,n); 
} 
void arrayin(int x[],int n) 
{ 
    for (int i = 0; i < n; i ++) 
    { 
     cin >> x[i]; 
    } 
} 
void arrayout(int x[], int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     cout << x[i] << "\t"; 
    } 
} 

このコードは機能します。問題は、配列が何も値を持たずに宣言されたときに、配列が過去のスタックに初期化されることです。可変長配列には値が必要です。したがって、後で配列を宣言してください。

誰かが何らかの理由なく下降しました。

編集:可変長配列はISO C++の一部ではありません。標準準拠のC++コードを書くには、g ++またはclangで-pedanticフラグを使用する必要があります。

あなたは2つの簡単な選択肢があります。固定長配列の場合はstd :: array、動的配列の場合はstd :: vectorを使用してください。ここで

+3

または 'std :: vector ' – WhozCraig

+0

を使用していただきありがとうございます。 – Qasimi

+2

私は理由を.. downvotedあなたの質問は、編集する前に、 "nの入力を取った後にあなたの配列を宣言する"で構成されています。これは本当の答えではありません、それはコメントです。あなたの編集後、私は自分のdownvoteを撤回します。しかし、IMOのこの答えはあなたが非標準拡張について言及しなかったので役に立たない。または実際のC++ソリューション –

5

は問題です:

int n, x[n]; // It is undefined behaviour 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 

正しい方法は、(可変サイズアレイの拡張子を持つコンパイラに)です:

int n; 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
int x[n]; 

C++を使用して正しい方法はstd::vectorを使用することです代わりに:

int n; 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
std::vector<int> x(n); 

を調整する必要があります。

3

は、問題はここにある:

int n; 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
int x[n]; // << now n has been initialized 
cout << "Please enter the elements: " << endl; 
arrayin(x,n); 

ところで::VLA(またはあなたがそれらを呼び出すと動的配列)が非C++での標準が、gccのある(とpossibily

int n, x[n]; // <-- n is not yet initialized 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
cout << "Please enter the elements: " << endl; 
arrayin(x,n); 

あなたはこれを必要としますclang)はそれらを拡張子として持つ。

2

int n, x[n];はあなたが不定値を持つことになりますnを宣言している問題

です。この値を使用すると、不確定なサイズの配列が宣言されます。

あなたがC++を使用して、そのユーザーの入力した後、あなたの配列を作成するnewキーワードを使用しています

cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
int *x = new int[n]; 
// your stuff 
delete x; 
関連する問題