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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

In this case you can now use the po<strong>in</strong>ter, ptr, just as you would the array name itself. I can<br />

hear you wonder<strong>in</strong>g, though: “But why would you want to?” The truth is that <strong>in</strong> this example<br />

there is no real benefit to us<strong>in</strong>g a po<strong>in</strong>ter. The real benefit of po<strong>in</strong>ters is when it comes to<br />

creat<strong>in</strong>g objects dynamically <strong>in</strong> memory. In that case, a po<strong>in</strong>ter is necessary to access the<br />

object. I really can’t go on with this discussion, though, until I digress a moment and talk<br />

about the two ways you can create variables and objects.<br />

Local Versus Dynamic Memory Usage<br />

So far all my sample programs have used local allocation of objects—that is, the memory<br />

required for a variable or object is obta<strong>in</strong>ed from the program’s stack.<br />

Local allocation means that the memory required for a variable or object is obta<strong>in</strong>ed<br />

from the program’s stack.<br />

NEW TERM<br />

NEW TERM<br />

The stack is an area of work<strong>in</strong>g memory set aside by the program when the program<br />

starts.<br />

Any memory the program needs for th<strong>in</strong>gs such as local variables, function calls, and so on<br />

is taken from the stack. This memory is allocated as needed and then freed when it is no longer<br />

needed. Usually this happens when the program enters a function or other local code block.<br />

Memory for any local variables the function uses is allocated when the function is entered.<br />

When the function returns, all of the memory allocated for the function’s use is freed. It all<br />

happens for you automatically; you don’t have to give any thought to how or if the memory<br />

is freed.<br />

Local allocation has its good po<strong>in</strong>ts and its bad po<strong>in</strong>ts. On the plus side, memory can be<br />

allocated from the stack very quickly. The downside is that the stack is of a fixed size and<br />

cannot be changed as the program runs. If your program runs out of stack space, weird th<strong>in</strong>gs<br />

start to happen. Your program might just crash, it might start behav<strong>in</strong>g oddly, or it might<br />

seem to perform normally but crash when the program term<strong>in</strong>ates. This is less of a problem<br />

<strong>in</strong> the 32-bit world than it is <strong>in</strong> 16-bit programm<strong>in</strong>g, but it’s still a consideration.<br />

For th<strong>in</strong>gs like variables of the built-<strong>in</strong> data types and small arrays, there is no po<strong>in</strong>t <strong>in</strong> do<strong>in</strong>g<br />

anyth<strong>in</strong>g other than local allocation. But if you are go<strong>in</strong>g to be us<strong>in</strong>g large arrays, structures,<br />

or classes, you will probably want to use dynamic allocation from the heap. This amounts to<br />

your free physical RAM plus all of your free hard disk space. In other words, you could easily<br />

have 100MB of heap memory available on a typical W<strong>in</strong>dows system. The good news here<br />

is that you have virtually unlimited memory available for your programs. The bad news is that<br />

memory allocated dynamically requires some additional overhead, and as such is just a<br />

smidgen slower than memory allocated from the stack. In most programs the extra overhead<br />

is not noticed <strong>in</strong> the least. An additional drawback of dynamic allocation is that it requires<br />

more from the programmer. Not a lot more, m<strong>in</strong>d you, but a little.<br />

69<br />

3

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

Saved successfully!

Ooh no, something went wrong!