私は宿題に取り組んでいます。私のクラスのインストラクターは質問をすることができません。彼は私たち自身でそれをすべて理解しようとしますが、私はこれを今週働かせようとしています。同じエラーを投げているのです。私は理由もありません。私はいくつかのGoogle検索を実行し、同様のエラーを持っていて解決策を見つけることができなかった人のためにそれを閲覧しました。新しい配列の作成時にアクセス違反の書き込み場所
それはそれは新しい配列を作成し、ユーザーの入力は、新しい配列を充填開始するために入力され、それがスローされるまで正常に動作します:0x000B517Bで
初回例外を6b.exeに:0xc0000005で:アクセス 違反の書き込み場所は0x00008147です。 '[4112] 6b.exe'プログラムはコード0(0x0)で終了しました。
#include "stdafx.h"
#include <iostream>
using namespace std;
int* read_data(int& size){
size++;
return &size;
}
void main(){
int size = 0; // size is 0 to start
int max = 10; // set max to 10 to start
float user; //user imputed float number
cout << "Start entering your numbers.\n Be sure to hit RETURN between each one.\nWhen you are finished, hit 'Q'\n";
float *a = new float [max]; //set first array that can be deleted and replaced
do {
//if the array is full, make a new one and replace.
if(size == max){
max = max *2; //double max number
float *b = new float [max]; //create temporary array b
for (int i = 0; i < size; i++){
b[i] = a[i]; //copy old array to temporary
}
delete[] a; //remove old a array.
float *a = new float [max]; //create new a array with the new max, same name for the loop.
//copy new a array to resume filling
for(int i = 0; i< size; i++){
a[i] = b[i];
}
delete[] b; //remove temporary array to free memory.
}
cin >> user; // user inputs the number
a[size] = user; //user input
read_data(size); //increase size by one.
}while (!cin.fail());
size--; //remove one for the entering of Q otherwise it throws off the count.
if (cin.fail()){
cout << "\n\nYou have finished inputting.\n You have imputed " << size << " numbers. \nThe inputed numbers are as follows:\n";
for(int i=0; i < size; i++){
cout << a[i];
if (i == size -1){
cout << "\n\n";
}
else {
cout << ", ";
}
}
cout << "\n\n";
}
system("pause");
}
インストラクターが非常に多く存在している理由である基本的にすべての行のコメントを望んでいます。
誰もがこれで私を助けることができれば素晴らしいだろう。
インデントに何か間違っています、 –
Stackoverflowはあなたの宿題をデバッグする場所ではありませんが、あなたの質問を下落させる人は適切ではないので。最初に失敗したコード行を特定する必要があります(私はすでにそれを行いましたが、 "無料"バグ修正には役に立たないでしょう)、 'cerr <<"コードを実行してください。 'cer'が常に出力されるので(' cout'はプログラムがクラッシュしたときにメッセージを出力しないことがあるので)最後の正しいコードを見つけます( 'cout'の代わりに' cerr'を使います)。デバッガが良いでしょうが、ターミナルにいますか? – GameDeveloper
SOを使用することは最後の手段でした。私はVisual Studio 2012を使用しています。次回はcerrを心に留めておきます。教えてくれてありがとう、教えてくれてありがとう。 – Comatose