An indeterminate amount of time after the corruption happened. That's almost as bad as not crashing ever. |
Well, again, you'd have to write the code poorly to get to this point. I don't consider it easy to corrupt your own memory - especially when modern compilers won't even allow you to use uninitialized variables.
That said, I do recall my 3-star programming professor write code so inconceivable, that somehow the creation of a class variable did not trigger a constructor call (I don't remember the details) and therefore a variable did not get initialized but was still used.
I don't think it was like this, but this example shows how it could happen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
class MyClass
{
public:
int a;
MyClass()
{
a = 0;
std::cout << "Constructor called" << std::endl;
}
void Display() {
std::cout << "Displaying: " << a << std::endl;
}
};
int main()
{
// Allocate raw memory for MyClass
char buffer[sizeof(MyClass)];
// Create a pointer to the raw memory
MyClass* myClassPtr = reinterpret_cast<MyClass*>(buffer);
myClassPtr->Display();
return 0;
}
|
This being one reason I recommend just setting the variables to a default value when you're declaring them in the class rather than writing out "a = 0;" in the constructor and wasting your time. I know that wouldn't work for this example, but it did work for the 3-star professor's code which I don't recall.
But you don't get to this point on accident, you have to go out of your way to do things the "wrong" (dangerous) way.
Also, I don't think I've used any language that didn't allow for race conditions (undefined behavior) when using multi-threading - arguably the easiest thing to make a mistake on while coding.
I think if you're coding so poorly that you cause serious and damaging undefined behavior, you were probably gonna shoot yourself in the foot with defined behavior too.