1

C++ の関数から配列を返したい。この簡単なコードを作成して、それを達成しようとしました。

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

動作しますが、次の警告が表示されます:

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

std::vector<int> a[2]静的にすると警告が解決するのはなぜですか?

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

ダングリング ポインターの警告なしで関数から配列を返す別の方法はありますか?

ありがとうございました。

4

2 に答える 2