1

I need my function to return an array, but it doesn't take an array as argument as most of search examples show.

The code is like this:

double  myfunction ()
{
    double arr[10];
    //assign values to the array
    return arr;
}

main()
{
    double arr2[10];
    arr2[10] = myfunction;
    //print arr2
}

When I used pointers to display the array, I got values like "CCCCCC"...

Storm2012
  • 549
  • 1
  • 9
  • 14
  • 1
    Arrays don't work like that. Using `std::array` or `std::vector` will give you behaviour like that though, but make sure you know how the raw arrays work as well. See this question: http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c – chris May 30 '12 at 01:20
  • This shouldn't even compile, a `double[10]` is not implicitly convertible to `double` in the same way that `double(*)()` is not implicitly convertible to `double` (which is an out-of-bounds access btw). Please show the real code. – Xeo May 30 '12 at 01:23

4 Answers4

6

You can't do that ... arrays are blocks of memory, and thus can't be "returned" ... You can pass pointers to arrays around through function calls, but you can't safely return pointers that are pointing to array memory blocks unless those arrays are declared static. Otherwise you will get undefined results as the memory on the stack occupied by the array will be reclaimed when the function returns, and the returned pointer to the stack memory location will be invalid. Using the returned pointer will most likely corrupt the stack, or crash your program.

In C you would have to explicitly use a pointer to a heap-allocated array in order to avoid this issue, but with C++ the best thing to use would be std::vector or std::array for a fixed-size array. For instance, std::vector still uses heap memory to store the array (not the stack), but via RAII the heap memory is safely managed by the object's lifetime. Therefore unlike C you won't have to worry about memory leaks from failing to free the memory pointed to by pointers returned from functions that are pointing to heap memory.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • 1
    Yes, but as I point out in my answer, that can create a nice memory leak if there is a question about pointer ownership and you fail to call `delete` on pointers you "own" the memory to. – Jason May 30 '12 at 01:26
  • I think `std::array` would be better here if applicable. It seems like a fixed-size array, and not millions of elements at that. – chris May 30 '12 at 01:27
  • Your second paragraph wasn't there when I wrote my earlier comment. And yes - there's a question of ownership: smart pointers are equally suitable for handling that specific concern as containers, but containers are better encapsulated and many manage their own resizing so are generally a better bet. – Tony Delroy May 30 '12 at 01:29
  • @TonyDelroy : Sorry, I've been updating the answer over the last couple minutes ... I didn't think of `std::shared_ptr`, but yes, that's definitely a possibility too. Pretty much anything that does pointer-lifetime management would work okay, although as you pointed out, a container would be better than just working with pointers. – Jason May 30 '12 at 01:31
  • Does `std::shared_ptr` do arrays? I thought it didn't because that's what `std::vector` is for. – chris May 30 '12 at 01:33
  • @chris : the pointer manged by `shared_ptr` can point to a heap-allocated memory block you can use as an array – Jason May 30 '12 at 01:35
  • *Not that you should do this, but*...as an aside, there is a `shared_array` in boost. It didn't make it into a C++ standard...though there are "custom deleters": http://stackoverflow.com/questions/627641/tr1-shared-arrays – HostileFork says dont trust SE May 30 '12 at 01:38
  • @Jason, oh, I see. Is it really logical to do that though? I mean, that's the whole reason for `std::vector`'s existence, which predated it altogether. – chris May 30 '12 at 01:41
1

Note: main must have result type int, by both the C and the C++ standards.

It's also a good idea to use const just about everywhere that it’s possible, because that adds constraints to the code which makes the code easier to understand (less to consider).

And finally, and most important, do use the standard library facilities.

#include <array>
using namespace std;

typedef array<double, 10> MyArray;

MyArray myfunction ()
{
    MyArray arr;
    //assign values to the array
    return arr;
}

int main()
{
    MyArray const arr = myfunction();
    //print arr2
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Example returning a std::vector.

#include <vector>

std::vector <int> some_numbers()
{
  return { 2, 3, 5, 7, 11, 13, 17, 19 };
}

Obviously you can assign values to the vector any way you wish. This example just returns a hard-coded list of primes.

You can use it anywhere any container for which begin and end work as usual:

#include <iostream>

int main()
{
  for (int x : some_numbers())
    std::cout << x << "\n";
}
Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
0

You can't return an built-in array or a function from a function. In other words, the return type of a function can't be a built-in array or a function type.

But you can return a pointer to either of those.

Other option is to use std::array or std::vector as the return type.

One example using std::array is shown below:

std::array<int, 5> func()
{
    std::array<int, 5> localArray{1,2,3,4,5};
    //do something with array 
    
    //return it
    return localArray;
}
Jason
  • 36,170
  • 5
  • 26
  • 60
  • It is possible to return a pointer to a function, or to return a functor, from a function. And both a pointer to a function or a functor can be used, syntactically, as if they are a function. – Peter Nov 19 '22 at 10:43
  • @Peter Note that C++ standard doesn't allow a function type or an array type as function's return type. – Jason Nov 19 '22 at 11:09