How do i make a function return an array?

  • Thread starter Thread starter okkvlt
  • Start date Start date
  • Tags Tags
    Array Function
Click For Summary
SUMMARY

This discussion focuses on returning arrays from functions in C++. It highlights that returning a local array directly is not feasible due to memory deallocation upon function termination. Instead, it recommends passing an array's pointer to the function or dynamically allocating memory using 'new'. Additionally, using C++ STL vectors is presented as a modern alternative for returning arrays, which simplifies memory management.

PREREQUISITES
  • C++ programming language fundamentals
  • Understanding of pointers and memory management
  • Familiarity with dynamic memory allocation using 'new'
  • Basic knowledge of C++ Standard Template Library (STL) vectors
NEXT STEPS
  • Explore C++ pointer arithmetic and memory management techniques
  • Learn about dynamic memory allocation and deallocation in C++
  • Investigate the performance implications of using STL vectors versus raw arrays
  • Study best practices for avoiding memory leaks in C++ applications
USEFUL FOR

C++ developers, software engineers, and students learning about memory management and data structures in C++.

okkvlt
Messages
53
Reaction score
0
in the c++ programming language, how do i make a function return an array?
 
Technology news on Phys.org
return array;

But you probably need a little more detail
It's more common to pass the address of the array to the function and let the function modify it directly.
 
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
1K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 23 ·
Replies
23
Views
2K