12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Us<strong>in</strong>g the Debugger<br />

The diagnostic macros are declared <strong>in</strong> a file called checks.h, so you need to add the l<strong>in</strong>e<br />

#<strong>in</strong>clude <br />

to your source code as well. If you neglect to <strong>in</strong>clude checks.h, you will get a compiler error<br />

on any l<strong>in</strong>es that conta<strong>in</strong> TRACE or WARN.<br />

Track<strong>in</strong>g Down GPFs<br />

NOTE<br />

All W<strong>in</strong>dows programmers have encountered general protection faults and general protection<br />

exceptions while develop<strong>in</strong>g their applications. For simplicity, I will refer to both<br />

universally as GPFs.<br />

The term GPF is a holdover from 16-bit W<strong>in</strong>dows. Its use is still<br />

prevalent <strong>in</strong> the 32-bit W<strong>in</strong>dows programm<strong>in</strong>g world even though<br />

32-bit W<strong>in</strong>dows actually generates access violation errors rather than<br />

general protection faults.<br />

GPFs can be difficult to track down for beg<strong>in</strong>n<strong>in</strong>g and experienced W<strong>in</strong>dows programmers<br />

alike. Often, as programmers ga<strong>in</strong> experience <strong>in</strong> writ<strong>in</strong>g W<strong>in</strong>dows programs, they develop<br />

a sixth sense of sorts regard<strong>in</strong>g locat<strong>in</strong>g the cause of GPFs. The follow<strong>in</strong>g sections describe<br />

some th<strong>in</strong>gs to look for when try<strong>in</strong>g to track down the elusive GPF. These are not the only<br />

situations that cause a program to crash, but they are some of the most common.<br />

Un<strong>in</strong>itialized Po<strong>in</strong>ters<br />

An un<strong>in</strong>itialized po<strong>in</strong>ter is a po<strong>in</strong>ter that has been declared but has not been set to po<strong>in</strong>t to<br />

anyth<strong>in</strong>g mean<strong>in</strong>gful <strong>in</strong> your program. An un<strong>in</strong>itialized po<strong>in</strong>ter will conta<strong>in</strong> random data.<br />

In the best case it po<strong>in</strong>ts to some harmless spot <strong>in</strong> memory. In the worst cases the un<strong>in</strong>itialized<br />

po<strong>in</strong>ter po<strong>in</strong>ts to a random memory location somewhere <strong>in</strong> your program. This can lead to<br />

erratic program behavior because the po<strong>in</strong>ter might po<strong>in</strong>t to a different memory location each<br />

time the program is run. Always set po<strong>in</strong>ters to NULL both before they are used for the first<br />

time and after the object po<strong>in</strong>ted to is deleted. If you try to access a NULL po<strong>in</strong>ter, your program<br />

will GPF, but the offend<strong>in</strong>g l<strong>in</strong>e <strong>in</strong> the source code will be highlighted by the debugger, and<br />

you can immediately identify the problem po<strong>in</strong>ter.<br />

421<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!