简体   繁体   中英

New to C++, Please help identify issue with code pointer code

Having issues with my getPosNums3 function.....all the others work as I need them to. I'm having issues in general understanding pointers, but I am sure that will pass. The aforementioned function spits out the size and address as I need it to, but when I print the newly modified array, it prints out long identical negative integers akin to this: -5476891, -5476891. It'll put out the right amount of integers, which tells me it is a small adjustment that is hanging up my code.....all these functions modify an array down to only its positive values; they just do so via different methods. I appreciate the help

#include <iostream>
using namespace std;

typedef int* IntArrayPtr;

int* getPosNums1(int* arr, int arrSize, int& outPosArrSize);
int* getPosNums2(int* arr, int arrSize, int* outPosArrSizePtr);
void getPosNums3(int* arr, int arrSize, int*& outPosArr, int& outPosArrSize);
void getPosNums4(int* arr, int arrSize, int** outPosArrPtr, int* outPosArrSizePtr);
void printNewArray(int* arr, int arrSize);
void fillArray(int a[], int size);

int main() {
    cout << "Fuction 1: " << endl;
    int array_size;
    cout << "What is the size of the array? ";
    cin >> array_size;
    IntArrayPtr a;
    a = new int[array_size];
    fillArray(a, array_size);
    int posArraySize;
    int* posNums1 = getPosNums1(a, array_size, posArraySize);
    cout << "Original array is: ";
    printNewArray(a, array_size);
    cout << "The new address is " << posNums1 << " and the new size is " << posArraySize << " " << endl;
    cout << "New array is: ";
    printNewArray(posNums1, posArraySize);
    delete[] a;
    cout << endl;

    cout << "Function 2: " << endl;
    int array_size2;
    cout << "What is the size of the array? ";
    cin >> array_size2;
    a = new int[array_size2];
    fillArray(a, array_size2);
    cout << "Original array is: ";
    printNewArray(a, array_size2);
    int* posArraySize2 = &array_size2;
    int* posNums2 = getPosNums2(a, array_size2, posArraySize2);
    cout << "The new address is " << posNums2 << " and the new size is " << *posArraySize2 << " " << endl;
    cout << "New array is: ";
    printNewArray(posNums2, *posArraySize2);
    delete[] a;
    cout << endl;


    cout << "Function 3: " << endl;
    int array_size3;
    cout << "What is the size of the array? ";
    cin >> array_size3;
    a = new int[array_size3];
    fillArray(a, array_size3);
    cout << "Original array is: ";
    printNewArray(a, array_size3);
    int* posNums3 = new int[array_size3];
    int posArraySize3 = array_size3;
    getPosNums3(a, array_size3, posNums3, posArraySize3);
    cout << "The new address is " << posNums3 << " and the new size is " << posArraySize3 << endl;
    cout << "New array is: ";
    printNewArray(posNums3, posArraySize3);
    delete[] a;
    cout << endl;

    cout << "Function 4: " << endl;
    int array_size4;
    cout << "What is the size of the array? ";
    cin >> array_size4;
    a = new int[array_size4];
    fillArray(a, array_size4);
    cout << "Original array is: ";
    printNewArray(a, array_size4);
    int* posNums4ptr = &array_size4;
    int* posNums4 = new int[array_size4];
    int** posNums4ptrptr = &posNums4;
    getPosNums4(a, array_size4, posNums4ptrptr, posNums4ptr);
    cout << "The new address is " << posNums4ptrptr << " and the new size is " << *posNums4ptr << endl;
    cout << "New array is: ";
    printNewArray(posNums4, *posNums4ptr);
    delete[] a;
    return 0;
}

int* getPosNums1(int* arr, int arrSize, int& outPosArrSize) {
    int* newArray = new int[arrSize];
    int counter = 0;

    for (int i = 0; i < arrSize; i++) {
        if (arr[i] > 0) {
            newArray[counter] = arr[i];
            counter++;
        }
    }
    outPosArrSize = counter;

    return newArray;
}

int* getPosNums2(int* arr, int arrSize, int* outPosArrSizePtr) {
    int size = 0, counter = 0;
    int newArraySize = 0;
    for (int i = 0; i < arrSize; i++) {
        if (*(arr + i) > 0) {
            newArraySize++;
        }
    }
    int* newArray = new int[newArraySize];

    for (int i = 0; i < arrSize; i++) {
        if (*(arr + i) > 0) {
            newArray[counter] = *(arr + i);
            size++;
            counter++;
        }
    }

    *outPosArrSizePtr = size;

    return newArray;
}

void getPosNums3(int* arr, int arrSize, int*& outPosArr, int& outPosArrSize){
    int counter = 0, size = 0;
    for (int i = 0; i < arrSize; i++) {
        if (*(arr + i) > 0) {
            size++;
        }
    }

    int *newArray = new int[size];

    for (int i = 0; i < arrSize; i++) {
        if (*(arr + i) > 0) {
            newArray[counter] = *(arr + i);
            counter++;
        }
    }

    delete[] outPosArr;
    outPosArr = newArray;

    outPosArrSize = size;

    delete[] newArray;
    newArray = nullptr;
}

void getPosNums4(int* arr, int arrSize, int** outPosArrPtr, int* outPosArrSizePtr){
    int size = 0, counter = 0, newArraySize = 0;
    for (int i = 0; i < arrSize; i++) {
        if (*(arr + i) > 0) {
            newArraySize ++;
        }
    }

    int* temp = new int[newArraySize];

    for (int i = 0; i < arrSize; i++) {
        if (*(arr + i) > 0) {
            temp[counter] = arr[i];
            size++;
            counter++;
        }
    }

    *outPosArrSizePtr = size;
    *outPosArrPtr = temp;
}

void printNewArray(int* arr, int arrSize) {
    for (int i = 0; i < arrSize; i++)
        cout << arr[i] << " ";
    cout << endl;
}

void fillArray(int a[], int size) {
    cout << "Enter " << size << " integers." << endl;
    for (int i = 0; i < size; i++)
        cin >> a[i];
}

The code in question is

void getPosNums3(int* arr, int arrSize, int*& outPosArr, int& outPosArrSize){
    
    // [snip] count number of positive entries in arr and call it "size"

    int *newArray = new int[size];

    // [snip] copy positive entries in arr into newArray

    delete[] outPosArr;
    outPosArr = newArray;

    outPosArrSize = size;

    delete[] newArray;
    newArray = nullptr;
}

First of all, it's not a good idea for this function to delete[] outPosArr because that presumes that outPosArr is either nullptr or something that was previously allocated with new[] . If it isn't one of those two things then this function has undefined behavior.

Somebody calling a function to copy numbers into an array is not typically going to also want the function to clean up some previous thing that may or may not have been in that array.

But your real problem is that you allocate memory for an array via int *newArray = new int[size] , copy stuff in, and then immediately deallocate that memory by calling delete[] newArray . This leaves outPosArr to point to what used to be an array.

Also, assigning nullptr to newArray at the end of the function does nothing, because newArray is going out of scope anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM