-1

I'm currently writing a function that returns an array but it keeps showing [Warning] address of local variable 'bestIdx' returned [-Wreturn-local-addr] while compiling. What does this mean?

Below is the function I wrote:

int *findMostPrefered(int toyCnt, int childrenCnt, int prefer[][20], bool toyNum[], bool childrenNum[]){
int max = prefer[0][0], bestIdx[2]={0};
for(int i=(childrenCnt-1); i>=0; i=i-1){
    if(childrenNum[i] == 0){
        for(int j=(toyCnt-1); j>=0; j=j-1){
            if(toyNum[j] == 0){
                if(prefer[i][j] >= max){
                    max = prefer[i][j];
                    bestIdx[0] = i;
                    bestIdx[1] = j;
                }
            }   
        }
    }
}
return bestIdx;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Lel
  • 21
  • 2
  • 5
    It means what it says - you are not returning the array, just a pointer to the local one which seizes to exists the moment you exit from the call. You want to return either `std::array` or `std::vector` depending on whether its size is known at compile-time. – Quimby Oct 10 '22 at 10:23
  • 2
    Or perhaps even a `std::pair` - or some custom defined `struct` that holds the two values you are returning – UnholySheep Oct 10 '22 at 10:25
  • 2
    _"writing a function that returns an array"_ - Just give that up. You can't return arrays in C++. You can return _one_ element only. It can otoh _contain_ an array. – Ted Lyngmo Oct 10 '22 at 10:28

1 Answers1

2

You declared the block scope array bestIdx with automatic storage duration

int max = prefer[0][0], bestIdx[2]={0};

that will not be alive after exiting the function. So the returned pointer to the first element of the array will be invalid. Dereferencing such a pointer invokes undefined behavior.

Instead of the array with two elements you could use an object of the type std::pair<int, int> and return it. For example

#include <utility>

//...

std::pair<int, int> findMostPrefered(int toyCnt, int childrenCnt, int prefer[][20], bool toyNum[], bool childrenNum[]){
int max = prefer[0][0];
std::pair<int, int> bestIdx( 0, 0 );
for(int i=(childrenCnt-1); i>=0; i=i-1){
    if(childrenNum[i] == 0){
        for(int j=(toyCnt-1); j>=0; j=j-1){
            if(toyNum[j] == 0){
                if(prefer[i][j] >= max){
                    max = prefer[i][j];
                    bestIdx.first = i;
                    bestIdx.second = j;
                }
            }   
        }
    }
}
return bestIdx;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you all for replying! A little question to ask to @Vlad from Moscow, how do I call the function in the main function? – Lel Oct 10 '22 at 10:55
  • @Lel You can write for example std::pair mostPrefered = findMostPrefered( /* required arguments */ ); or auto mostPrefered = findMostPrefered( /* required arguments */ ); – Vlad from Moscow Oct 10 '22 at 10:57
  • Thank you! But is it possible to call each value in the array in the main function? For example, I want to make mostPrefered1 = the first value of the array, and mostPrefered2 = the second value of the array? – Lel Oct 10 '22 at 11:01
  • @Lel I already showed how to access elements of the pair: mostPrefered.first and mostPrefered.second. – Vlad from Moscow Oct 10 '22 at 11:19
  • Got it! Thank you so much for replying and answering! – Lel Oct 11 '22 at 12:54
  • @Lel You can close your question by selecting the best answer and your reputation will be increased.:) – Vlad from Moscow Oct 11 '22 at 12:59