user393964
user393964

Reputation:

Function returning an array of enums

I need a method that returns an array of enums and I tried:

note_name *AppSettings::getNoteMap(){
    note_name notes[] = {..}
}

I read somewhere on SO that I'm supposed to return the address of the first element of the array, so that's why there's a pointer. This is giving me a warning though: address of stack memory associated with local variable notes returned

How do I get rid of the warning and return the array properly?

Upvotes: 2

Views: 1826

Answers (4)

Ed Swangren
Ed Swangren

Reputation: 124652

You cannot return an array from a function, period. You can return a pointer to a dynamically allocated chunk of memory (I wouldn't go this route), or take a pointer (along with the size) as an output argument, or return a pointer to a static array, or return a vector<T> or some other collection.

I would use a std::vector or a std::array.

std::vector<note_name> foo() {
    std::vector<note_name> ret;
    // populate 'ret'
    return ret;
}

Upvotes: 3

Eric Niebler
Eric Niebler

Reputation: 6177

Let's look at what you wrote:

note_name *AppSettings::getNoteMap(){
    note_name notes[] = {..}
}

The array notes is a local variable that will be cleaned up automatically when the function returns. If you return a pointer to it, then you're returning a pointer to an array that no longer exists. That's why the compiler is complaining.

If the size and contents of the array are known at compile time, declare the array static const. Then you can return a reference to the array. The syntax is a bit weird:

static const std::size_t note_names_size = 42;
note_name const (&AppSettings::getNoteMap())[note_names_size] {
    static note_name const notes[note_names_size] = { ... };
    return notes;
}

If the size of the array is known at compile-time, but not the contents, use a std::array. You can declare a local variable, fill it in, and return it by value.

If the size and contents of the array are not known at compile time (or if your library doesn't implement std::array), use a std::vector instead.

Upvotes: 0

ForEveR
ForEveR

Reputation: 55887

Use std::vector, std::array, some other collection or dynamic-array (i.e. use new, instead of allocation on stack), since after exit from function-scope your array will be destroyed and you will have pointer, that points on some garbage in memory. I think that you knows about how many values in your enum and if you have C++11 you can use std::array.

std::array<note_name> foo() {
   std::array<note_name, size> ret = { ... }; // fill ret, instead of ...
   return ret;
}

Upvotes: 1

mathematician1975
mathematician1975

Reputation: 21351

To do it this way you would need to allocate the array dynamically and return the pointer that gets returned by the corresponding call to new. You cannot return a pointer to a variable that is local to the function as it will cease to exist after the function exits. Using new will ensure the memory persists on the heap after the function exits. As you are using C++ you would be much better served by using std::vector and make your function return an instance of a vector

Upvotes: 1

Related Questions