How do i make a function return an array?

  • Thread starter Thread starter okkvlt
  • Start date Start date
  • Tags Tags
    Array Function
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 4K views
okkvlt
Messages
53
Reaction score
0
in the c++ programming language, how do i make a function return an array?
 
Physics news on Phys.org
If you declare an array locally inside a function, and then return it, you're actually returning a pointer to the data which was allocated inside the function. The problem is, when the function terminates, its local variables become undefined! More precisely, their memory is released and is likely to be re-used for local variables in the next function that you call.

If you want/need to stick with arrays, there are two ways around this:

1. Declare the array in the calling function, and pass a pointer to the function. Then you can either return the pointer,

Code:
#include <iostream>
#include <cmath>

using namespace std;

double *roots (double *x, int n)
{
    for (int k = 0; k < n; ++k)
        x[k] = sqrt(k);
    return x;
}

int main ()
{
    double numbers[5];
    double *sqrts;
    sqrts = roots (numbers, 5);
    for (int k = 0; k < 5; ++k)
        cout << sqrts[k] << endl;
    return 0;
}

or simply use the pointer parameter in the calling function, after the function call:

Code:
#include <iostream>
#include <cmath>

using namespace std;

void roots (double *x, int n)
{
    for (int k = 0; k < n; ++k)
        x[k] = sqrt(k);
}

int main ()
{
    double numbers[5];
    roots (numbers, 5);
    for (int k = 0; k < 5; ++k)
        cout << numbers[k] << endl;
    return 0;
}

2. Allocate the memory for the array dynamically using 'new' inside the function. Then it stays valid after the function terminates, and you can safely return a pointer to it. But you need to remember to 'delete' that memory at some point, in general, or your program is likely to spring a memory leak.

Code:
#include <iostream>
#include <cmath>

using namespace std;

double *roots (int n)
{
    double *x = new double[n];
    for (int k = 0; k < n; ++k)
        x[k] = sqrt(k);
    return x;
}

int main ()
{
    double *sqrts;
    sqrts = roots (5);
    for (int k = 0; k < 5; ++k)
        cout << sqrts[k] << endl;
    delete[] sqrts;
    return 0;
}

Or you can use a vector instead. You can declare a vector inside a function, and return it in a "return" statement, which copies the vector to the calling function. The potential drawback here is the time it takes to copy the vector, if it's large or you have to do it many times in a loop.

Code:
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

vector<double> roots (int n)
{
    vector<double> x(n);
    for (int k = 0; k < n; ++k)
        x[k] = sqrt(k);
    return x;
}

int main ()
{
    vector<double> sqrts;
    sqrts = roots (5);
    for (int k = 0; k < 5; ++k)
        cout << sqrts[k] << endl;
    return 0;
}
 
Last edited: