mercredi 13 novembre 2019

How do I delete a dynamically allocated array that is initialized in another function?

int* generateArray(int size)

This function should dynamically create an array and should return the created array to the array generated in the main program.

int main() 

{

    int *numList = generateArray(501);
    cout << "Mode = " << findMode(arr, 501) << endl;

    cout << "Median = " << findMedian(arr, 501);

    delete[] numList;
    numList = nullptr;

    return 0;
}

I also need to delete the dynamically allocated array. I wanna make sure if I deleted the new pointer properly. By deleting the generated in the at the end of int main would it delete the new pointer in the function as well?

int *generateArray(int size) 

{

    srand(time(0));

    int *arr = new int[size];

    for (int i=0; i<size; i++) 
    {
        arr[i] = rand() % 91 + 10;
    }
    return arr;
}

Aucun commentaire:

Enregistrer un commentaire