Search code examples
c++arrayssortingpointerspointer-arithmetic

Passing arrays and pointers with access violation


I am working on an assignment which must pass pointers for all function parameters. No global variables are allowed except global constants.

I'm to create an array of "bids" in main and fill it with readBids() function. This works, but I am then supposed to pass it to a function to bubble sort it. My program breaks once my sortBids function is called. I'm learning pointers now and I can't see what I am doing wrong. The Call Stack gives Project4.exe!main()Line32, which points to sortBids(bidArray, numBids);

Any help and an explanation would be very appreciated.

 #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);
}

Solution

  • Problem:

    You intialise swap to 0. As swap is a pointer to bool, you have a null pointer.

    You later dereference this pointer without ever having it point to a valid bool object:

       *swap = true;
    

    Tha is UB and this is why you get an access violation !

    Solution

    Either you define this variable as plain object bool swap = false; and use swap everywhere. Or you initialize it correctly bool *swap = new bool{false}; and you use *swap everywhere.

    Miscellaneous advice:

    Attention: bidArray is allocated with new[], so you have to delete[] it or risk undefined behaviour !

    In pointer definitions, take the habit of puting the star next to the variable and not to the type. Why ? Because optically it is confusing:

     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