gpt4 book ai didi

c++ - Valgrind - 错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:50 25 4
gpt4 key购买 nike

下面是我在 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()

;

最佳答案

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

double val;

你应该将其更改为

double val = double();

double val(0);

(我根据大小假设了 double;它也可能是 long long 或某种其他类型的大小 8)。

关于c++ - Valgrind - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com