0

I have a question for you... I have a function in c++ that returns a double pointer:

double * Calc_ToF_low::CalcToF(int16_t* señal, int fs){


    double ToF_est [4]={0,0,0,0};
    ToF_est[0]=time_est(result2,fs);
    ToF_est[1]=ToF_est[0];
    ToF_est[2]=ToF_est[0];
    ToF_est[3]=ToF_est[0];


    return(ToF_est);

And in main:

double *ToF_est;
ToF_est=ToFobject.CalcToF(señal,fs);

And when i do:

cout<<ToF_est[0]<<endl;

Not has the same value... why??

Thanks

haccks
  • 104,019
  • 25
  • 176
  • 264
Telo Pena Barreiro
  • 277
  • 1
  • 6
  • 15

1 Answers1

3

You can't return a pointer to automatic local variables. It invokes undefined behavior. Once the function returns, ToF_est is no longer exist.
If you want to return a pointer then use dynamic allocation (new).

double *ToF_est = new double [4];
haccks
  • 104,019
  • 25
  • 176
  • 264