sircrisp
sircrisp

Reputation: 1067

How do I return a dynamically allocated pointer array from a function?

I am now starting Dynamic Memory Allocation in class and have a ok understanding of it but can't completely use it properly. I feel like I may not be so great with pointers either :p

My instructor gave instructions to create a function named readArray that will prompt the user for a number to use as a size to dynamically create a integer array of that size. I am then to assign the new array to a pointer. I then am supposed to prompt the user to fill the array. I then am supposed to return both the newly created array and the size.

I can not figure out how to return the array though, and I thought when dynamically allocating memory you were supposed to delete the allocation after using it to prevent leaks.

The array and size must be returned to main so I can pass it to other functions such as a sorting function.

I would greatly appreciate any help I can get as my thought process with this keeps going in the wrong direction.

#include <iostream>
using namespace std;

int* readArray(int&);
void sortArray(int *, const int * );

int main ()
{
   int size = 0;
   int *arrPTR = readArray(size);
   const int *sizePTR = &size;
   sortArray(arrPTR, sizePTR);

   cout<<arrPTR[1]<<arrPTR[2]<<arrPTR[3]<<arrPTR[4];

        system("pause");
        return 0;
}


int* readArray(int &size)
{
   cout<<"Enter a number for size of array.\n";
   cin>>size;
   arrPTR = new int[size];

   for(int count = 0; count < (size-1); count++)
   {    
       cout<<"Enter positive numbers to completely fill the array.\n";
       cin>>*(arrPTR+count);
   }

   return arrPTR;
}

Upvotes: 1

Views: 21534

Answers (3)

crashmstr
crashmstr

Reputation: 28573

Like amit says, you should probably return the array instead of size. But since you still need the size, change readArray like so:

///return array (must be deleted after)
///and pass size by reference so it can be changed by the function
int* readArray(int &size);

and call it like this:

int size = 0;
int *arrPTR = readArray(size);
///....Do stuff here with arrPTR
delete arrPTR[];

After update:

int* readArray(int size); ///input only! need the & in the declaration to match
                          ///the function body!

Is wrong, since you have your actual definition with the int &size. You also don't declare arrPTR in readArray, just assign it.

Upvotes: 1

amit
amit

Reputation: 178451

To return an array: declare readArray() as int* readArray() [return an int* instead of an int], and return arrPTR instead of size. This way, you return the dynamically allocated array which arrPTR points to.

Regarding the delete: When you are done using the array, you should indeed delete it. In your example, do it before return 0 in your main() function.
Make sure that since you allocated memory with new[], you should also free it with delete[], otherwise - your program will have a memory leak.

Upvotes: 5

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361472

You would not need to do that if you use std::vector<int> which is far superior choice.

Use it:

std::vector<int> readArray()
{
    int size = 0;
    cout<<"Enter a number for size of array.\n";
    cin >> size;
    std::vector<int> v(size);

    cout<<"Enter "<< size <<" positive numbers to completely fill the array : ";
    for(int i = 0; i < size; i++)
    {   
        cin>> v[i];
    }
    return v;
}

Upvotes: 6

Related Questions