odd C++ bug(?)

Started by
24 comments, last by NukeCorr 14 years, 1 month ago
Okay, I have no idea what's happening here.

...
#define DEFAULT_KILLSMSG 6
...
int killsMsgWep[DEFAULT_KILLSMSG];
...
// init()
// this for() loop, crashes the program
// but if I replace -1 with 0 or something else, it does not crash
for(int i=0;i<DEFAULT_KILLSMSG;i++)
{
   killsMsgWep = -1;
}
...
// then i decided to check which of them crashes it
killsMsgWep[0] = -1;
killsMsgWep[1] = -1; // <--- this one, when it's -1, it crashes the program
killsMsgWep[2] = -1;
killsMsgWep[3] = -1;
killsMsgWep[4] = -1;
killsMsgWep[5] = -1;
...

Those are the only places "killsMsgWep" variable is used, nowhere else. So how can the initializion crash the program with such simple variable? Am I missing something here or what's this about :S
What the h*ll are you?
Advertisement
Quote:Original post by NukeCorr
Okay, I have no idea what's happening here.

*** Source Snippet Removed ***
Those are the only places "killsMsgWep" variable is used, nowhere else.
So how can the initializion crash the program with such simple variable?
Am I missing something here or what's this about :S


That works completely fine when run in isolation, what compiler are you using? I'm guessing there must be something else in your code causing this, perhaps memory has been corrupted somewhere else in your code.
Best regards, Omid
I'm using MSVC++ 6.0 (yes I know I should get the latest one).

If it's corrupted memory, how do I fix or prevent that, and what might be causing it?

EDIT: the program has over 40 files of source, could that affect in it too?
What the h*ll are you?
Is that array defined globally like your example indicates, or is it actually part of a class or something in your actual code?
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Quote:Original post by grekster
Is that array defined globally like your example indicates, or is it actually part of a class or something in your actual code?


it's part of a class under public:
What the h*ll are you?
Quote:Original post by NukeCorr
Quote:Original post by grekster
Is that array defined globally like your example indicates, or is it actually part of a class or something in your actual code?


it's part of a class under public:


And the init() function is too? In that case is the instance of the class you're calling init() on valid? if not then that could easily cause a crash.

If init() isn't part of the class, what instance are you accessing the array on?
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
init() is in the same class as the array, and the class has many other defined variables and functions (bool, int, etc). And the others work just fine, but this new array I added causes failure
What the h*ll are you?
Yes but is the actual instance of the class you are calling init() on valid?

Also it may be helpful to show some more actual code, like the whole of the init function plus where it gets called from.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Sorry, I don't understand your question..

anyway, I removed the array just to test does the rest of the code work and now it doesn't, it did earlier though.
When I run debugger it gives me this:
http://img17.imageshack.us/img17/2447/acsv.png
What the h*ll are you?
It seems to me like the class instance you are actually calling init on is invalid, possibly a null pointer or just uninitialized e.g.:

MyClass myInstance;
myInstance.init(); // <-- good, valid instance of the class!

MyClass* myInstance;
myInstance->init(); // <---- bad! uninitialized pointer, not a real instance of 'MyClass'

Quote:Also it may be helpful to show some more actual code, like the whole of the init function plus where it gets called from.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!

This topic is closed to new replies.

Advertisement