-1

以下是我在 Valgrind 上运行代码时收到的消息

1) 未初始化的值是由堆栈分配创建的 ==16808== 在 0x400EC1: init() (Hite.cpp:90)

2) 大小为 8 的无效读取

xmin=0.000000, xmax=1.000000, dx=0.010000, dt=0.000208, c=3.000000
==2763== Conditional jump or move depends on uninitialised value(s)
==2763==    at 0x3E5420E836: __ieee754_exp (in /lib64/libm-2.12.so)
==2763==    by 0x3E54224A81: exp (in /lib64/libm-2.12.so)
==2763==    by 0x400F88: init() (Hite.cpp:96)
==2763==    by 0x4010C8: main (Hite.cpp:114)
==2763==  Uninitialised value was created by a stack allocation
==2763==    at 0x400EC1: init() (Hite.cpp:90)
==2763==
==2763== Use of uninitialised value of size 8
==2763==    at 0x3E5420E8EC: __ieee754_exp (in /lib64/libm-2.12.so)
==2763==    by 0x3E54224A81: exp (in /lib64/libm-2.12.so)
==2763==    by 0x400F88: init() (Hite.cpp:96)
==2763==    by 0x4010C8: main (Hite.cpp:114)
==2763==  Uninitialised value was created by a stack allocation
==2763==    at 0x400EC1: init() (Hite.cpp:90)

我们如何克服出现的这些错误

90 void init() {
 91         double xmin=0;
 92     double xavg = 0.5*(xmin+xmax);


int main(int argc,char **argv) {
112     printf("xmin=%f, xmax=%f, dx=%f, dt=%f, c=%f\n",
113                     xmin=0,xmax=0,dx=0,dt=0,c=0);
114     init()

;

4

1 回答 1

1

查看代码会有所帮助,但从外观上看,您在堆栈上创建了一个未初始化的对象:

double val;

你应该把它改成

double val = double();

或者

double val(0);

(我假设double基于尺寸;它也可能是long long8 号或其他类型的尺寸)。

于 2013-09-27T15:16:07.827 回答