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.

Up to Your Neck <strong>in</strong> <strong>C++</strong><br />

If you are attempt<strong>in</strong>g to allocate very large chunks of memory (several<br />

megabytes <strong>in</strong> size) or are try<strong>in</strong>g to allocate memory at critical po<strong>in</strong>ts <strong>in</strong><br />

your program, you should check the po<strong>in</strong>ter for validity before cont<strong>in</strong>u<strong>in</strong>g.<br />

For rout<strong>in</strong>e memory-allocation chores, you can probably get<br />

by without check<strong>in</strong>g to ensure that the new operator succeeded.<br />

delete<br />

All memory allocated must be deallocated (released or freed) after you are done with the<br />

memory. With local objects, this happens for you automatically, and you don’t have to worry<br />

about it. The memory manager allocates the memory your object needs from the stack and<br />

then frees that memory when the object goes out of scope (usually when a function returns<br />

or when the code block <strong>in</strong> which the object was declared ends). When us<strong>in</strong>g dynamic memory<br />

allocation, the programmer must take the responsibility of free<strong>in</strong>g any memory allocated with<br />

the new operator.<br />

NEW TERM<br />

WARNING<br />

Free<strong>in</strong>g memory allocated with new is accomplished with the delete operator.<br />

All calls to new need to have a match<strong>in</strong>g delete. If you do not free all<br />

memory allocated with the new operator, your program will leak<br />

memory. You need to be diligent <strong>in</strong> match<strong>in</strong>g new/delete pairs.<br />

Us<strong>in</strong>g the delete operator is frightfully easy:<br />

SomeObject* myObject = new SomeObject;<br />

// do a bunch of stuff with myObject<br />

delete myObject; // so long!<br />

That’s all there is to it! There isn’t a lot to the delete operator, but there are a couple of th<strong>in</strong>gs<br />

about po<strong>in</strong>ters and delete that you should be aware of. The first is that you must not delete<br />

a po<strong>in</strong>ter that has already been deleted, or you will get access violations and all sorts of other<br />

fun stuff. Second, it is okay to delete a po<strong>in</strong>ter that has been set to 0. So what does that mean<br />

<strong>in</strong> the real world? Let me expla<strong>in</strong>.<br />

Sometimes you declare a po<strong>in</strong>ter just <strong>in</strong> case it might be used, but you don’t know for sure<br />

whether it will be used <strong>in</strong> a given <strong>in</strong>stance of your program. For example, let’s say you have<br />

an object that is created if the user chooses a certa<strong>in</strong> menu item. If the user never chooses that<br />

83<br />

3

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

Saved successfully!

Ooh no, something went wrong!