未初始化的值是由堆栈分配 Valgrind c++ 创建的
Uninitialised value was created by a stack allocation Valgrind c++
所以,当 运行 命令:--track-origins=yes
在 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)
第 500 行是:
printf("Iteration = %d t = %.10f p_iter = %d\n", iter, time[iter+1], p_iter);
第 336 行是:
double time[iter_max + 1];
我不知道这是什么意思?这次是如何造成内存泄漏问题的?
主要代码示例
#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);
}
这不是泄密。您正在读取未初始化的内存。正如@PaulMcKenzie 在评论中所说,time
不是标准的 C++,而是 GCC extension that comes from C99.
您可以使用 memset
来确保 time
已初始化。或者,您可以使用 std::vector
,例如
std::vector<double> time(iter_max + 1, 0.0);
这将创建一个大小为 iter_max + 1
的向量,全部初始化为 0.0。
但是,就目前而言,除了在 printf
中,您的代码没有使用 time
变量,因此您可以简单地删除它。
所以,当 运行 命令:--track-origins=yes
在 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)
第 500 行是:
printf("Iteration = %d t = %.10f p_iter = %d\n", iter, time[iter+1], p_iter);
第 336 行是:
double time[iter_max + 1];
我不知道这是什么意思?这次是如何造成内存泄漏问题的?
主要代码示例
#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);
}
这不是泄密。您正在读取未初始化的内存。正如@PaulMcKenzie 在评论中所说,time
不是标准的 C++,而是 GCC extension that comes from C99.
您可以使用 memset
来确保 time
已初始化。或者,您可以使用 std::vector
,例如
std::vector<double> time(iter_max + 1, 0.0);
这将创建一个大小为 iter_max + 1
的向量,全部初始化为 0.0。
但是,就目前而言,除了在 printf
中,您的代码没有使用 time
变量,因此您可以简单地删除它。