2

If I had the following program:

#include <iostream>

// being lazy and using all std just for example.
using namespace std;

int (*some_func())[10]
{
    int arr[10]= {0,1,2,3,4,5,6,7,8,9};

    return arr;
}

int main()
{
    int arr2[10] = some_func();
    for(auto i : arr2)
        cout << i << endl;

    return 0;
}

is this the correct way to define a function that returns an array? I know I am missing something vital here, my knowledge tells me that when you return arr, you get a *to the first element of arr, but it also tells me that arr is defined locally meaning that arr will be undefined when the function finishes hence when I actually assign arr to arr2 from the return I am assigning it undefined memory which is not what I want, yet if I return &arr isn't that essentially the same?

Any assistance on understanding this in relation to the above example would be useful, It seems I have confused myself somewhere but I'm not quite sure how to rectify it.

Thanks,

Euden

4
  • 4
    Are you forced to use arrays or could you instead return a std::vector? Commented Apr 14, 2013 at 8:13
  • Did you try actually compiling the code? My brain-compiler says you'll get an error on the int arr2[10] = some_func(); line. And also on the return arr; line, together with a warning for good compilers. Commented Apr 14, 2013 at 8:14
  • 2
    If you are using 'array' in the traditional sense, as it is used in C, then there is no correct way to return an array from a function. At least not directly. You can return a pointer to an array, or a pointer to an array's first element. Or you can return a class with an array as a member. But you cannot return an array, because that requires that the array be copy-constructible, which it is not. Commented Apr 14, 2013 at 8:18
  • This was essentially an experiment on the built in array types to see if what methods are available to return them, in reply to Micha, I am not forced to, more specifically I would like to know if there is a way for this to work, I understand that using std::array or std::vector would work just fine. Commented Apr 14, 2013 at 8:25

1 Answer 1

7

In C++11 you should use container std::array

#include <iostream>
#include <array>

std::array<int, 10> get_array()
{
    return {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}

int main()
{
    auto arr = get_array();
    for(const auto& elem: arr)
        std::cout << elem << std::endl;
}

Why is it preferable?

  1. In C++ you can't return an array, you can return pointer to the first element. Look at the code:

    int* array()
    {
        // int arr[10]; // <-- DO NOT USE IT!
        int* arr = new int[10]; // Better, but still ugly
        return arr;
    }
    
    int main()
    {
        auto arr = array();
    }
    

    But what about memory? You should delete array after using, but you can forget about it. So, you need a smart pointer.

    std::unique_ptr<int[]> array()
    {
        std::unique_ptr<int[]> arr(new int[10]);
        return arr;
    }
    

    NOTE: Both realizations uses dynamically allocated arrays, while std::array encapsulate static array.

  2. You can't copy or assign arrays like other objects, you need to iterate over one array and copy data into another. std::array container allows you to forget about it.

    int main()
    {
        std::array<int, 10> arr1{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
        std::array<int, 10> arr2;
    
        arr2 = arr1;
    
        arr1.at(4) = 42;
    
        // arr1: {1, 2, 3, 4, 42, 6, 7, 8, 9, 10}
        // arr2: {1, 2, 3, 4, 5,  6, 7, 8, 9, 10}
    }
    
  3. std::array provides a lot of useful member functions like an other container, for example - std::array::at(pos).

    int main()
    {
        std::array<int, 10> arr;
        arr.at(42) = 42;
    }
    

    It will throw an exception, if pos >= arr.size().

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

3 Comments

It's noteworthy that your return line not only requires std::array, but also uniform-initialization, which some compilers (namely MSVC) don't support yet.
I understand that essentially there is no real way to return the built in type arrays without real fiddling which is a shame but if this works then thats fine with me. :)
@BenCrazedUpEuden, I've updated the post with some information, why is std::array preferable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.