So, I get this error when running the command: --track-origins=yes
on Valgrind
==57680== Conditional jump or move depends on uninitialised value(s)
==57680== at 0x100584A6B: __dtoa (in /usr/lib/system/libsystem_c.dylib)
==57680== by 0x1005AB2DD: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==57680== by 0x1005D1AD2: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==57680== by 0x1005AA2B1: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==57680== by 0x1005A841D: printf (in /usr/lib/system/libsystem_c.dylib)
==57680== by 0x100002466: main (test.cpp:500)
==57680== Uninitialised value was created by a stack allocation
==57680== at 0x100002025: main (test.cpp:336)
line 500 is:
printf("Iteration = %d t = %.10f p_iter = %d\n", iter, time[iter+1], p_iter);
and line 336 is:
double time[iter_max + 1];
I don't know what this means? How this time is creating a memory leak issue?
main code example
#include<math.h>
#include<stdio.h>
#include "Functions.h"
#include "fftw3.h"
#include <cstring>
int main(){
// Setting parameters
double frequency = 200.0;
double dt = 0.1;
double tend = 5000.;
double err_max = 1e-7;
int P_iter_max = 500;
int iter_max = tend / dt;
int saveNumber = 1;
//Initialize Parameters
double *ksqu;
ksqu = (double*) fftw_malloc(nx*nyk*sizeof(double));
memset(ksqu, 42, nx*nyk* sizeof(double)); //initialize for now
fftw_complex *Pek;
Pek = (fftw_complex*) fftw_malloc(nx*nyk* sizeof(fftw_complex));
memset(Pek, 42, nx*nyk* sizeof(fftw_complex));
fftw_complex *Pik;
Pik = (fftw_complex*) fftw_malloc(nx*nyk* sizeof(fftw_complex));
memset(Pik, 42, nx*nyk* sizeof(fftw_complex));
fftw_complex *nuink;
nuink = (fftw_complex*) fftw_malloc(nx*nyk* sizeof(fftw_complex));
memset(nuink, 42, nx*nyk* sizeof(fftw_complex));
//similarly initialize everything else
//Save I.Cs
int p_iter = Potk(invnk, dndxk, dndyk, Pk, PSk, kx, ky, ninvksq, err_max, P_iter_max);
double time[iter_max + 1];
time[0] = 0;
for (int iter = 0; iter < iter_max; iter++){
calcPSk(dndxk, dndyk, Pik, Pek, nuink, nuiek, nuenk, nueik, isigPk, Oci, Oce, u, B, ksqu, potSourcek);
}
printf("Iteration = %d t = %.10f p_iter = %d\n", iter, time[iter+1], p_iter);
}
This isn't a leak. You are reading uninitialized memory. As @PaulMcKenzie says in the comments, time
is not standard C++, rather it is a GCC extension that comes from C99.
You could use memset
to ensure that time
is initialized. Alternatively, you could use std::vector
, for instance
std::vector<double> time(iter_max + 1, 0.0);
This will create a vector of size iter_max + 1
all initialized to 0.0.
However, as it stands, your code is not using the time
variable other than in the printf
, so you could simply remove it.