Game Development Reference
In-Depth Information
pointer, it is equivalent to executing an assembly level JMP statement, which simply
moves the execution path to a different statement.
In C++, objects can be declared inside local scopes such as for loops. In normal exe-
cution, these objects are destroyed when execution passes out of that scope. The C++
compiler inserts the appropriate code to do this, and you can
t see it unless you look
at a disassembly window. What do you suppose happens to C++ objects that go out
of scope if you skip important lines of code? Let
'
'
s look at an example:
class MyClass
{
public:
int num;
char* str;
MyClass(int const n)
{
num = n;
str = new char[128];
sprintf(str,
%d
, n);
}
~MyClass() { delete str; }
};
void SetTheIP()
{
char buffer[2048];
buffer[0] = 0;
for (int a = 0; a < 128; ++a)
{
MyClass m(a);
strcat(buffer, m.str); // START HERE...
}
} // JUMP TO HERE...
Normally, the MyClass object is created and destroyed once for each run of the for
loop. If you jump out of the loop using Set Next Statement, the destructor for
MyClass never runs, leaking memory. The same thing would happen if you jumped
backward to the line that initializes the buffer variable. The MyClass object in scope
won
'
t be destroyed properly.
 
Search WWH ::




Custom Search