初始化列表使变量未初始化?

发布于 2024-09-09 12:10:43 字数 1465 浏览 9 评论 0原文

我有一个只有这样的构造函数的类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

染墨丶若流云 2024-09-16 12:10:52

如果您有疑问,只需添加调试 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.

咿呀咿呀哟 2024-09-16 12:10:50

我发现它:

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 like gettimeofday(). 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 used getSpeedFactor() to assign counter, so that way counter get a value of -10000...

Thanks all.

淡紫姑娘! 2024-09-16 12:10:50

在我的脑海中,您可能需要定义具有适当初始值设定项的私有默认值和复制构造函数。没有使用过 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.

初心 2024-09-16 12:10:48

没有足够的代码来重现问题。

通用 SO/开发者论坛建议:

提供重现问题的最小代码片段。

通常(根据我的经验,大约有 85% 的情况)减少代码片段的过程已经为您发现了错误。


编辑:您的添加仍然没有给出问题的可编译示例,但至少有足够的信息来识别该问题 - 或者至少其中之一:

Game::Game() : /* Some other stuff .... */  , view(new IntroScreen(this))`
{}

我不确定是否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:

Game::Game() : /* Some other stuff .... */  , view(new IntroScreen(this))`
{}

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 initialized this at this point, so chances are your IntroScreen constructor does bogus things.

新人笑 2024-09-16 12:10:47

您是否不小心用未初始化的局部变量隐藏了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文