2016-02-11 34 views
0

我正在處理一個必須通過所有功能 參數指針的分配。除全局常量外,不允許使用全局變量。通過數組和指針訪問衝突

我要在main中創建一個「出價」數組,並用readBids()函數填充它。這有效,但我應該把它傳遞給一個函數來對其進行冒泡排序。一旦我的sortBids函數被調用,我的程序就會中斷。我現在正在學習指針,我看不出我做錯了什麼。 Call Stack給出Project4.exe!main()Line32,其指向sortBids(bidArray, numBids);

任何幫助和解釋將不勝感激。

#include <iostream> 
    #include <string> 

    using namespace std; 

    string* readProductName(); 
    int* readNumBids(); 
    double* readBids(string,int); 
    void sortBids(double*, int*); 
    void averageBid(); 
    void maxBid(); 
    void totalBid(); 
    void printReport(); 


    int main(){ 
     string* productName; 
     int* numBids; 


     productName = readProductName(); 
     numBids = readNumBids(); 
     double* bidArray = readBids(*productName, *numBids); 
     sortBids(bidArray, numBids); 


     cout << *productName << " " << *numBids << endl; 
     for (int i = 0; i < *numBids; i++){ 
      cout << bidArray[i] << endl; 
     } 
     system("PAUSE"); 
     delete productName; 
     delete numBids; 
     delete bidArray; 
     return 0; 
    } 

    string* readProductName(){ 
     string* productName = new string; 
     cout << "\n Please enter a product name\n"; 
     cin >> *productName; 

     return productName; 
    } 

    int* readNumBids(){ 
     int* numBids = new int; 
     cout << "\n Please enter the number of bids\n"; 
     cin >> *numBids; 

     return numBids; 

    } 

    double* readBids(string productName, int numBids){ 
     int* size = new int; 
     size = &numBids; 
     string* productNamePtr = new string; 
     productNamePtr = &productName; 

     double *bidArray; 
     bidArray = new double[*size]; 

     cout << "\nHow many bids for the " << *productNamePtr << endl; 
     for (int i = 0; i < *size; i++){ 
      cout << "Please enter bid #" << i + 1 << endl; 
      cin >> bidArray[i]; 
      if (bidArray[i] <= 0){ 
       cout << "\nPlease enter an amount larger than 0\n"; 
       i--; 
      } 
     } 
    return bidArray; 
} 

void sortBids(double* array, int *size){ 
    bool* swap = bool{ false }; 
    double* temp = new double; 

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

你已經看到太多的Java和太少的C++。 '新的雙';而這就是瘋狂。 – LogicStuff

+0

這很有可能 – sircrisp

+0

**必須通過所有函數參數的指針**爲什麼在地球上? – SergeyA

回答

0

問題:

您intialise swap爲0。swap是一個指向bool,你有一個空指針。

您稍後取消引用該指針時無需將它指向一個有效的布爾對象:

*swap = true; 

臨屋是UB,這就是爲什麼你會得到一個訪問衝突!

解決方案

要麼你到處定義這個變量作爲普通對象bool swap = false;和使用swap。或者您正確初始化它bool *swap = new bool{false};並且您在任何地方都使用*swap

雜項建議:

注意:比達雷分配與new[],所以你必須delete[]它或風險不確定的行爲!

在指針定義中,習慣把星形放在變量旁邊而不是類型。爲什麼?因爲光學上它令人困惑:

bool* a,b; // defines a pointer to bool a, but a PLAIN BOOL b ! 
bool *a,b; // invites otpically to right interpretation by human reader 
+0

OP的其他原因擺脫所有指針 – NathanOliver

+0

我糾正了這一點,但它並沒有解決我原來的問題。 @NathanOliver我很想擺脫指針,但它是這項任務的要求。 – sircrisp

+0

@sircrisp這份任務應該教給你什麼?討厭C++? – LogicStuff