Valgrind内存错误:未初始化的值 [英] Valgrind memory error: uninitialized value

查看:182
本文介绍了Valgrind内存错误:未初始化的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我的C ++项目中有以下代码:

Hi all!

I have following code in my C++ project:

object *d = reinterpret_cast<object *>(it -> second);
d -> type = MINIGAMEZ_TYPE_OBJECT;
d -> value = map<string, void *>();



其中it -> second是在malloc()的帮助下分配的内存.
之后,我的代码中的另一个函数正在使用此内存.使用valgrind进行测试时,它表示我正在使用未初始化的值.因为这只是我的项目中的错误,所以我想摆脱它.



where it -> second is memory allocated with the help of malloc().
After that this memory is being used by another functions in my code. When testing with valgrind it says that I am using uninitialized values. Since this is only error in my project I would like to get rid of it.

推荐答案

语录:由于这只是我的项目中的错误,我希望得到摆脱它..

您究竟希望我们怎么做才能做到这一点?如果您使用的一个或多个尚未初始化的值,则需要修复程序以对其进行更正.我们无法猜测错误可能在哪里.
Quote: "Since this is only error in my project I would like to get rid of it. ".

And what exactly do you expect us to do to make that happen? If you are using a value or values that have not been initialised then you need to fix your program to correct it. We cannot guess where the error may be.


下面的答案是在查看用户发布的原始代码后发布的.他更改了代码,这毫无意义.


如果问题出在您发布的摘录的第一行中,这也不会令我感到惊讶.您正在将指针投射到对象.尝试用static_cast替换reinterpret_cast,然后修复编译器告诉您的错误.

[实际上,在大多数我使用的编译器上,我怀疑第一行是否会在高警告下进行编译并将警告视为错误(/W4/WX).尝试将警告级别提高一点,然后看看会发生什么.]

哦,除非您要链接的C库要求对内存进行malloc,否则不要使用malloc.

干杯,

The following answer was posted looking at the original code the user posted. It''s pretty meaningless as he''s changed the code.


It wouldn''t surprise me if the problem was in the first line of the snippet you''ve posted. You''re casting a pointer to an object. Try replacing the reinterpret_cast with a static_cast and then fixing what the compiler tells you is wrong.

[Actually on most of the compiler I use I doubt the first line would compile on high warnings and treat warnings as errors (/W4 /WX). Try cranking the warning level up a bit and see what happens.]

Oh, and don''t use malloc unless a C library you''re linking with requires the memory to be malloced.

Cheers,

Ash


这篇关于Valgrind内存错误:未初始化的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆