I have a function which creates a very large std::vector. When returning this vector, I do not want to copy it again, due to its size. Therefore, I want to return a reference to that vector. However, doing so gives me a segmentation fault. Why is this? What is the solution?
Here is my code:
std::vector<int>& Foo()
{
std::vector<int> x(100000, 50);
return x;
}
int main()
{
std::vector<int> y = Foo();
return 0;
}