2015-04-02 73 views
1

我正在使用一個int指針作爲私人訪問數組。當我爲存儲編寫單獨的函數並獲取數組的值時,程序崩潰。但是,如果我在構造函數中編寫get值和存儲值代碼,程序工作正常。我無法找到問題所在。使用指針作爲私人類訪問C++中的數組

計劃1:(不工作)

#include<iostream> 

using namespace std; 

class NewArray{ 
private: 
    int Size = 0; 
    int *arrAddr = NULL; 

public: 
    NewArray(int); 
    void SetValue(int); 
    void GetValueOf(int); 
}; 

//Array is created 
NewArray::NewArray(int arSz){ 
    int arr[arSz]; 
    Size = arSz; 
    arrAddr = arr; 
    cout << "An array of Size " << Size << " is created" << endl; 
} 

// Store Value function 
void NewArray::SetValue(int index) 
{ 
    cin >> *(arrAddr+(index)); 
} 

//Get value function 
void NewArray::GetValueOf(int idx) 
{ 
    if ((idx >= Size) || (idx < 0)) 
    { 
     cout << "index value is out of bound" << endl; 
    } 
    else 
    { 
     cout << *(arrAddr+idx) << endl; 
    } 
} 

int main() 
{ 
    int arrSize, arrIdx; 

    cout << "enter the size of array" << endl; 
    cin >> arrSize; 

    if (arrSize > 0) 
    { 
     NewArray ar(arrSize); 

     cout << "enter " << arrSize << " values. Enter the values one after the other." << endl; 
     for (int i = 0; i < arrSize; i++) 
     { 
      ar.SetValue(i); 
      ar.GetValueOf(i); 
     } 

     cout << "enter the index to fetch the value" << endl; 
     cin >> arrIdx; 
     ar.GetValueOf(arrIdx); 
    } 
    else{ 
     cout << "invalid input" << endl; 
    } 
    return 0; 
} 

方案2:(代碼這是工作)

#include<iostream> 

using namespace std; 
// size is passed 

class NewArray{ 
private: 
    int Size; 
    int *arrAddr; 

public: 
    NewArray(int); 
    void GetValueOf(int); 
}; 

NewArray::NewArray(int arSz){ 
    int arr[arSz]; 
    int idx; 
    Size = arSz; 
    arrAddr = arr; 
    cout << "An array of Size " << Size << " is created" << endl; 

// Storing values in array 
    cout << "enter " << Size << " values. Enter the values one after the other." << endl; 
    for (int i = 0; i < Size; i++) 
    { 
     cin >> *(arrAddr+i); 
    } 

// To get the value from the index 
    cout << "enter the index to fetch the value" << endl; 
    cin >> idx; 

    if ((idx >= Size) || (idx < 0)) 
    { 
     cout << "index value is out of bound" << endl; 
    } 
    else 
    { 
     cout << "The value is " << *(arrAddr+idx) << endl; 
    } 
} 


int main() 
{ 
    int arrSize, arrIdx; 

    cout << "enter the size of array" << endl; 
    cin >> arrSize; 

    if (arrSize > 0) 
    { 
     NewArray ar(arrSize); 
    } 

    else{ 
     cout << "invalid input" << endl; 
    } 

    return 0; 
} 

我已經嘗試了這個特殊的例子,程序1在數組大小爲10時崩潰,以及當我試圖寫入第7個索引時。

任何人都可以請幫我找出原因嗎?

+0

看看我編輯的答案... – 2015-04-02 07:57:05

回答

2

在構造函數NewArray::NewArray()中,您將創建一個存儲在堆棧中的數組。離開構造函數後,它的生命週期結束,它將從堆棧中移除,因此通過指針arrAddr訪問它是未定義的行爲。

要簡單地解決問題,您需要使用newdelete將數組分配給堆,或將其存儲爲類成員。

這些只是兩種實施方式。我不要推薦什麼,他們只是的可能性

+0

雖然你的回答是正確的,但我不喜歡你的建議。 Exxept對於非常罕見的用例沒有任何理由,爲什麼要使用new和delete在堆上創建一個c樣式的數組。 – MikeMB 2015-04-02 08:21:06

+0

希望我的編輯修復了您的投訴。 – Downvoter 2015-04-02 08:40:58

+0

謝謝你..我想,當一個數組是使用構造函數創建的,它的生命週期將一直持續到對象被刪除。現在,爲什麼我的程序崩潰是有道理的。 – 2015-04-03 16:02:37

0

忘了int *。改爲使用vector。看here

#include <iostream> 
#include <conio.h> 
#include <vector> 

using namespace std; 

class Class1 { 
private: 
    vector<int> intArr; 
public: 
    void Set(int iValue) { 
     intArr.push_back(iValue); 
    } 
    int Get(int iIndex) { 
     return intArr[iIndex]; 
    } 
}; 

int main() { 
    Class1 class1; 
    for (size_t i = 0; i < 10; i++) 
    { 
     class1.Set(i); 
    } 
    for (size_t i = 0; i < 10; i++) 
    { 
     cout << class1.Get(i) << endl; 
    } 
    _getch(); 
} 
1

INT ARR [arSz];

在堆棧上分配,因此它的生存期限制在它定義的函數中 - 一旦堆棧解開,分配的堆棧內存可供其他人使用。您需要使用new運算符在堆上分配內存,以便內存在函數調用後保持持久性。

arrAddr = new int [arSz];

上堆上面分配內存和可用,直到明確給delete [] arrAddr一個電話,在大多數情況下只應在析構函數來完成刪除。

+0

謝謝你。這有助於我理解某些事情。 – 2015-04-03 16:04:37