I am trying to return an array of doubles. When my function returns it, and then I try to read from it in int main
it is telling me the value is 0.
We see this in the screenshot below.
My minimal verifiable example is:
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
double* pi_sequence(const int& len)
{
vector<double> ests(len);
double first = 3.123;
ests[0] = first;
cout << "first " << first << " ests.at:" << ests.at(0) << endl;
return ests.data();
}
int main() {
double* ests = pi_sequence(1);
std::cout << "after returned: " << ests[0] << std::endl;
}
Can you please help me understand why it is 0 after the return, and why it doesn't stay.