2012-02-16 11 views
0

私はクラスについてのポインタとnew演算子について学んでいます。ユニット化されたローカル変数と助けを訂正

私のreadArray関数では、サイズを読み込みます。サイズをに動的に使用する整数配列を作成します。次に、配列にポインタを代入し、それを埋め込み、サイズと配列を返します。

私はその部分を修正して修正したと思いますが、配列をソートしようとすると、エラー"初期化されていないローカル変数tempが使用されます。

問題は、私がそれを初期化しようとしているときにエラーが発生することです。 ご協力いただきありがとうございます。私の間違いを見ることは私にとって非常に役に立ちます。

#include <iostream> 
using namespace std; 

int* readArray(int&); 
void sortArray(int *, const int *); 

int main() 
{ 
    int size = 0; 
    int *arrPTR = readArray(size); 
    const int *sizePTR = &size; 
    sortArray(arrPTR, sizePTR); 

    cout<<arrPTR[1]<<arrPTR[2]<<arrPTR[3]<<arrPTR[4]; 

     system("pause"); 
     return 0; 
} 


int* readArray(int &size) 
{ 
    cout<<"Enter a number for size of array.\n"; 
    cin>>size; 
    int *arrPTR = new int[size]; 

    for(int count = 0; count < (size-1); count++) 
    { 
     cout<<"Enter positive numbers to completely fill the array.\n"; 
     cin>>*(arrPTR+count); 
    } 

    return arrPTR; 
} 

void sortArray(int *arrPTR, const int *sizePTR) 
{ 
    int *temp; 
    bool *swap; 

    do 
    { 
     swap = false; 
     for(int count = 0; count < (*sizePTR - 1); count++) 
     { 
      if(arrPTR[count] > arrPTR[count+1]) 
      { 
       *temp = arrPTR[count]; 
       arrPTR[count] = arrPTR[count+1]; 
       arrPTR[count+1] = *temp; 
       *swap = true; 
      } 
     } 
    }while (swap); 
} 

答えて

2

tempをポインタとして宣言しました。間接参照の前にヒープに割り当てる必要があります。しかし、おそらくスタック上の変数が望ましいでしょうか?

FYI:私は、これは他の問題のいくつかをクリアに役立つことを願っています:あなたはdelete []

編集を呼び出す責任発信者を残しされているだけでなくreadArrayでのメモリリークに注意する必要があります。

#include <iostream> 

int* readArray(int&); 
void sortArray(int*, int); 

int main() 
{ 
    int size(0); // use stack when possible 
    int *arrPTR = readArray(size); 
    sortArray(arrPTR, size); 

    // arrays are zero based index so loop from 0 to size 
    for (int index(0); index < size; ++index) 
     std::cout << arrPTR[index]; 

    delete [] arrPTR; // remember to delete array or we have a memory leak! 
    // note: because we did new[] for an array we match it with delete[] 
    //  if we just did new we would match it with delete 

    system("pause"); 
    return 0; 
} 

int* readArray(int& size) 
{ 
    std::cout << "Enter a number for size of array.\n"; 
    std::cin >> size; 
    int *arrPTR = new int[size]; // all news must be deleted! 

    // prefer pre-increment to post-increment where you can 
    for(int count(0); count < size; ++count) 
    { 
     std::cout << "Enter positive numbers to completely fill the array.\n"; 
     std::cin >> arrPTR[count]; 
    } 

    return arrPTR; 
} 

// passing size by value is fine (it may be smaller than pointer on some architectures) 
void sortArray(int *arrPTR, int size) 
{ 
    // you may want to check if size >= 2 for sanity 

    // we do the two loops to avoid going out of bounds of array on last iteration 
    for(int i(0); i < size-1; ++i) // the first to compare (all except last) 
    { 
     for(int j(i+1); j < size; ++j) // the second to compare (all except first) 
     { 
      // do comparison 
      if (arrPTR[i] > arrPTR[j]) // from smallest to biggest (use < to go from biggest to smallest) 
      { 
       // swap if needed 
       int temp(arrPTR[i]); // put this on stack 
       arrPTR[i] = arrPTR[j]; 
       arrPTR[j] = temp; 
      } 
     } 
    } 
} 
+0

OP 'temp'はポインタでなければならないと述べましたか?これはポインタではなく、このコードの 'int'です。 –

+0

'temp 'は質問の中のポインタでなければならないが、あなたの答えの新しいコメントによって今はおそらくいくつかの"興味深い要件 "が与えられていて、あなたがそれをカバーしていたことを参照してください;-) – AJG85

0

一時ポインタを初期化していない場合は、メモリのランダムな部分に書き込んでいるときに逆参照します。 Tempはポインタである必要はありませんが、intであってもかまいません。 * tempのすべてのインスタンスをtempに置き換えてください。

1

tempは、あなたが*temp = ...を言うとき、あなたが実際にtempを指していることを起こるものは何でもに代入しているが、あなたがそれを語っていないのでに何を指すように。あなたはないを初期化しているintへの「ポインタであり、 、それはかなりどこでもあなたのプログラムのアドレス空間に書き込むことができます。

をので、あなたがそれらを使用している方法で、単なるintbooltempswapは全くのポインタであるべきではないと思われます。

3

あなたはtempをint poにしますinter(初期化されていない)し、arrPTR [ccount]に何かを指すものを設定します。あなたはスワップするためだけにtempを使用しているので、スワップされたものと同じ型でなければなりません。この場合はintです。

それ絶対には、ポインタ(このため正当な理由なしにはありません、それは、混乱、遅いですエラーの可能性を追加し、メモリリークの可能性を追加します)でなければならない場合:

int *temp = new int; //make an int for the pointer to point at 
bool *swap = new bool; //make an bool for the pointer to point at 
do 
{ 
    //your code 
}while (swap); 
delete temp; 
delete swap; 
+0

これは私です何らかの理由でインストラクターが指導者に指示したと言っていました。私の顔はあなたのアバターと同じです。 – sircrisp

+0

私はあなたのインストラクターがtemp変数を特に言及していたとは思わない。それはできますが、あなたはそれをするべきではありません。ちょうど愚かなことです。 –

+0

tempとboolはまさに彼が言及しているものです。私はそれがもっと難しくなると思う:] – sircrisp

関連する問題