You forgot to initialize "int x".
You forgot to initialize "int x".
J-P Nurmi
xtreme (5th August 2008)
Thanks, it seems to be the solution. I always thought when defining a standart variable like a int that there was no need to add =0 to it. Still a bit odd that when using qDebug() it does not crash.
Welcome to the C/C++ joys of undefined behaviour.
Your variable x was not initialized. Its value therefore 'random', which means: whatever value the stack had at the location reserved for x.
Calling qDebug() seems to have had a side effect of leaving some value >= 0 at that position. (Pure coincidence...).
Without qDebug() the value happend (pure good luck again) to be negative (I guess) which lead to a crash in accessing mod[x].
You can and should not depend on something like that ;-)
Your compiler should have printed a warning for the forgotten initialization. Try to fix all warnings. Bad bugs tend to hide behind and among them. It takes way less time to fix the warnings than to fix the bugs hiding there afterwards.
And code that compiles with lots of warnings give an unpleasant feeling and lowers trust in that code. (I would not want to deliver code that compiles with thousands of warnings...)
HTH
Bookmarks