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.

82 Day 3<br />

As mentioned <strong>in</strong> the preced<strong>in</strong>g section, memory <strong>in</strong> a <strong>C++</strong> program is allocated dynamically<br />

us<strong>in</strong>g operator new. You free memory us<strong>in</strong>g the delete operator. Unless you have previously<br />

programmed <strong>in</strong> C, you might not appreciate the simplicity of new and delete. In C programs,<br />

you use malloc(), calloc(), realloc(), and free() to dynamically allocate memory.<br />

W<strong>in</strong>dows really complicates th<strong>in</strong>gs by offer<strong>in</strong>g a whole raft of local and global memoryallocation<br />

functions. Although this is not exactly difficult, it can be confus<strong>in</strong>g to say the least.<br />

<strong>C++</strong> removes those headaches through the use of new and delete.<br />

A new World Order<br />

NOTE<br />

You’ve already seen new <strong>in</strong> action, so let’s review. As discussed earlier, you can allocate<br />

memory locally (from the stack) or dynamically (from the heap). The follow<strong>in</strong>g code snippet<br />

shows examples of allocat<strong>in</strong>g two character arrays. One is allocated from the stack (local<br />

allocation), and the other is allocated from the heap (dynamic allocation):<br />

char buff[80];<br />

char* bigBuff = new char[4096];<br />

In the first case the buffer size is <strong>in</strong>significant, so it doesn’t really matter whether the stack<br />

or the heap is used. In the second case a large char array is needed, so it makes sense to allocate<br />

it from the heap rather than from the stack. This preserves stack space. In the case of arrays<br />

(remember, a str<strong>in</strong>g is just an array of type char), the dynamic and local flavors can be used<br />

<strong>in</strong>terchangeably. That is, they use the same syntax:<br />

strcpy(buff, “Ricky Rat”);<br />

strcpy(bigBuff, “A very long str<strong>in</strong>g that goes on and on...”);<br />

// later on...<br />

strcpy(bigBuff, buff);<br />

Remember that the name of an array when used by itself po<strong>in</strong>ts to the first memory location<br />

of the array. A po<strong>in</strong>ter also po<strong>in</strong>ts to the first memory location of the array, so that is why the<br />

two forms can be used <strong>in</strong>terchangeably.<br />

If the new operator fails to allocate the requested memory, it returns<br />

NULL. In theory, you should check the po<strong>in</strong>ter after call<strong>in</strong>g new to ensure<br />

that it conta<strong>in</strong>s a non-zero value:<br />

char* buff = new char[1024];<br />

if (buff) strcpy(buff, “Buteo Regalis”);<br />

else ReportError(); // someth<strong>in</strong>g went wrong<br />

In reality, if the new operator fails <strong>in</strong> a 32-bit W<strong>in</strong>dows program, the<br />

entire system is <strong>in</strong> trouble, and neither your program nor any other will<br />

be runn<strong>in</strong>g for long.

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

Saved successfully!

Ooh no, something went wrong!