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"...

2
  • 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: stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c Commented May 30, 2012 at 1: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. Commented May 30, 2012 at 1:23

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
I think std::array would be better here if applicable. It seems like a fixed-size array, and not millions of elements at that.
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.
@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.
Does std::shared_ptr do arrays? I thought it didn't because that's what std::vector is for.
|
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
}

Comments

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";
}

Comments

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;
}

2 Comments

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 Note that C++ standard doesn't allow a function type or an array type as function's return type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.