传递具有访问冲突的数组和指针

时间:2016-02-11 21:10:30

标签: c++ arrays sorting pointers pointer-arithmetic

我正在进行一项必须为所有函数传递指针的赋值 参数。除全局常量外,不允许使用全局变量。

我要在main中创建一个“出价”数组,并用readBids()函数填充它。这是有效的,但我应该将它传递给一个函数来冒泡它。一旦调用了sortBids函数,我的程序就会中断。我现在正在学习指针,我看不出我做错了什么。调用堆栈提供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);
}

1 个答案:

答案 0 :(得分:0)

<强>问题:

您将swap初始化为0.由于swap是指向bool的指针,因此您有一个空指针。

您稍后取消引用此指针,而不指向有效的bool对象:

   *swap = true;

Tha是UB,这就是您违反访问权限的原因!

<强>解决方案

要么将此变量定义为普通对象bool swap = false;,请在任何地方使用swap。或者,您可以正确初始化bool *swap = new bool{false};,并在任何地方使用*swap

其他建议:

注意:bidArray已分配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