1

I want to return an array from a function in C++. I made this simple code to try to achieve it.

#include <iostream>
#include <vector>

std::vector<int> *getx()
{
   std::vector<int> a[2];
   a[0].push_back(0);
   a[1].push_back(1);
   return a;
}

int main()
{
   std::vector<int>* b = getx();
   return 0;
}

It works but I get this Warning:

warning C4172: returning address of local variable or temporary: a

Why if i made std::vector<int> a[2] static I solve the warning?

static std::vector<int> a[2];

Is there another way to return an array from a function without having dangling pointers warnings?

Thank you.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
pquintans
  • 13
  • 2
  • 1
    Use `std::vector>` or `std::array, 2>` instead of plain arrays, which can't be returned from functions. – user17732522 Feb 01 '22 at 16:07
  • 1
    The problem with example 1 is returning a pointer to the `vector` rather than the `vector` itself. Never return a pointer to a local variable. The sucker goes out of scope and the pointer points to garbage. If you're thinking "Ermagehrd! All that copying!" you can [move it or count on Copy Elision](https://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion-return-statement) – user4581301 Feb 01 '22 at 16:13
  • Don't return a *pointer* to a `vector`, just return the `vector`. Likewise, the caller should not be a *pointer* to the returned `vector`, it just just be a `vector`. – Eljay Feb 01 '22 at 16:16
  • Do you really need an array of exactly two vectors? – n. m. could be an AI Feb 01 '22 at 16:21

2 Answers2

0

It works but I get this Warning:

Variables with automatic storage duration are automatically destroyed at the end of the scope where they are declared.

You return a pointer to an element of the automatic array and hence the returned pointer will be invalid. The behaviour of indirecting through such invalid pointer would be undefined.

Why if i made std::vector a[2] static I solve the warning?

Because if you make it static, then the variable has static storage duration rather than automatic storage duration. Hence, the array isn't destroyed at the end of its scope, and hence a pointer to its element will remain valid even after the function returns.

Is there another way to return an array from a function without having dangling pointers warnings?

It isn't possible to return an array in C++. It is however possible to return class objects, and classes can contain arrays as members. As such, you could return an instance of a class that contains the array that you want to return. There is a template for such array wrapper class in the standard library. It's called std::array.

Example:

std::array<std::vector<int>, 2> getx()
{
   return {
       std::vector<int>{0},
       std::vector<int>{1},
   };
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    The downside to making the `vector` `static` is you now have one and only one instance of the `vector` for all to use and abuse. If one changes it, it changes for everybody. – user4581301 Feb 01 '22 at 16:15
0

Why if i made std::vector a[2] static I solve the warning?

static std::vector a[2];

You may not return a pointer to a local array with automatic storage duration because the array will not be alive after exiting the function and as a result the returned pointer will be invalid.

But you may return a pointer to a local array with static storage duration because it will be alive after exiting the function.

However in fact there is no need to deal exactly with an array. Either use std::vector<std::vector<int>> or std::array<std::vector<int>, 2> as the return type of the function.

For example

std::vector<std::vector<int>> getx()
{
   std::vector<std::vector<int>> a( 2 );

   a[0].push_back(0);
   a[1].push_back(1);

   return a;
}

or

std::array<std::vector<int>, 2> getx()
{
   std::array<std::vector<int>, 2> a;

   a[0].push_back(0);
   a[1].push_back(1);

   return a;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335