初始化列表使变量未初始化?
我有一个只有这样的构造函数的类:
IntroScreen::IntroScreen(Game *game) :
View(game), counter(0.0f), message(-1), continueAlpha(255),
continueVisible(false), screenAlpha(255), fadeIn(false), fadeOut(false)
{
}
在方法的某个地方我有这个 if 语句
if (counter > 10.0f)
Valgrind 对于该行说:
条件跳转或移动取决于未初始化的值
但我在初始化列表中初始化了它!我想我相信 Valgrind。因为,有时一切都正确,有时什么也没有发生......所以,也许 counter
得到一个错误的值,所以需要很长时间才能计数器达到 10。
我已经检查了我使用 counter 的代码一些错误。但我认为你不能用 C++ 语句“取消初始化一个值”...
这些是我使用 counter
的所有行(初始化列表中除外):
counter += speed;
counter = 20.0f;
counter += game->getSpeedFactor();
if (counter >= 15.f)
counter = 15.f;
if (counter > 10.0f)
Valgrind 给出相同的输出对于screenAlpha
。
这两个变量都是 private
并且我没有 friend
类......
那么,这是怎么回事?可能是什么问题?
编辑:
我打印了值:
在构造函数中,它是正确的:0
按照我的方法,这是垃圾。它打印随机值,例如:
-97298.8...
-106542.2...
print 语句是该方法的第一行,其中所有分配给 counter 都在。
第二次编辑:
这可能是问题所在吗??
在我的 Game 类中,我像这样初始化 IntroScreen:
Game::Game() : /* Some other stuff .... */ , view(new IntroScreen(this))`
{}
这里的 view 是指向 IntroScreen 的抽象超类型的指针/code> 称为 View
。
I have a class with the only constructor like this:
IntroScreen::IntroScreen(Game *game) :
View(game), counter(0.0f), message(-1), continueAlpha(255),
continueVisible(false), screenAlpha(255), fadeIn(false), fadeOut(false)
{
}
And somewhere in a method I have this if-statement
if (counter > 10.0f)
And Valgrind says for that line:
Conditional jump or move depends on uninitialised value(s)
But I initialized it in my initializer list! And I think I believe Valgrind. Because, sometimes everything goes correct and sometimes nothing happens.... So, maybe counter
gets a wrong value and so it takes long until the counter reaches 10.
I already check my code where I use counter for some errors. But I think you can't "un-initialize a value" with a C++ statement...
These are ALL the lines (except in the initializer list) where I use counter
:
counter += speed;
counter = 20.0f;
counter += game->getSpeedFactor();
if (counter >= 15.f)
counter = 15.f;
if (counter > 10.0f)
Valgrind gives the same output for screenAlpha
.
Both variables are private
and I have no friend
classes....
So, what is going on? What the problem could be?
Edit:
I printed the value out:
In the constructor, it was correnct: 0
In my method, it was rubbish. It prited random values like:
-97298.8...
-106542.2...
The print statement is the first line of the method where all assignments to counter
are in.
Second Edit:
Can this be the problem!!??
In my Game
class, I initialize that IntroScreen
like this:
Game::Game() : /* Some other stuff .... */ , view(new IntroScreen(this))`
{}
view
is here a pointer to an abstract super-type of IntroScreen
called View
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有疑问,只需添加调试 printf 语句或等效语句即可。但这次我不会相信 Valgrind。
顺便说一句:删除不会“取消初始化”值。它删除了对象,但指针仍然指向该内存位置——它确实有一个值。
Just add a debugging printf statement or equivalent, if you have doubts. But I would not believe Valgrind this time.
BTW: delete does not "un-initliase" a value. It deletes object, but the pointer still points to that memory location — it does have a value.
我发现它:
getSpeedFactor()
仅在我第一次调用它时返回一个完全错误的数字,因为像gettimeofday()
这样的时间函数。起始值(更新游戏所花费的时间)被设置为初始化为零,停止值是一天中的微秒:它给出了全天的时间而不是更新时间。一旦游戏循环运行一次,错误的值就会被纠正(因为起始值被分配)。但是第一次执行游戏逻辑时,我使用getSpeedFactor()
来分配counter
,这样counter
就会得到一个值 - 10000...谢谢大家。
I found it:
getSpeedFactor()
returns only the first time I call it a complete wrong number because of time-functions likegettimeofday()
. The start value (to time how long it took to update the game) is set initialized to zero and the stop value is micros of the day: which gives a time of the whole day instead of the update time. Once the game loop ran once, the wrong value is corrected (because of the start value gets assigned). But the first time the game-logic was executed, I usedgetSpeedFactor()
to assigncounter
, so that waycounter
get a value of -10000...Thanks all.
在我的脑海中,您可能需要定义具有适当初始值设定项的私有默认值和复制构造函数。没有使用过 valgrind,但忘记做是很常见的事情。
Off the top of my head, you may need to define private default and copy constructors that have proper initializers. Haven't used valgrind, but it's a common thing to forget to do.
没有足够的代码来重现问题。
通用 SO/开发者论坛建议:
提供重现问题的最小代码片段。
通常(根据我的经验,大约有 85% 的情况)减少代码片段的过程已经为您发现了错误。
编辑:您的添加仍然没有给出问题的可编译示例,但至少有足够的信息来识别该问题 - 或者至少其中之一:
我不确定是否
new()
调用在初始化列表中甚至是合法的。但我确信此时您还没有完全初始化的this
,因此您的IntroScreen
构造函数很可能会做一些虚假的事情。Not enough code to reproduce problem.
Generic SO / developer forum advice:
Do provide a minimal code snippet reproducing the problem.
Quite often (about 85% of all cases in my experience) the process of reducing the code snippet already uncovers the bug for you.
Edit: Your addition still doesn't give a compilable example of your problem, but enough information at least to identify then problem - or, at least, one of them:
I am not sure whether a
new()
call is even legal in an initializer list. But I am sure that you do not have a fully initializedthis
at this point, so chances are yourIntroScreen
constructor does bogus things.您是否不小心用未初始化的局部变量隐藏了
counter
?否则,valgrind 可能会在已删除的对象中对此进行中间诊断(可能使用哨兵值)。
或者 valgrind 可能是错误的。
Did you accidentally shadow
counter
with a local variable that's uninitialized?Otherwise, it's possible that valgrind is mid-diagnosing this in an object that was already deleted (using sentry values perhaps).
Or valgrind could just be wrong.