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.

70 Day 3<br />

NEW TERM<br />

NEW TERM<br />

Dynamic allocation means that memory required for an object is allocated from the<br />

heap.<br />

The heap <strong>in</strong> a W<strong>in</strong>dows program refers to all of your computer’s virtual memory.<br />

Dynamic Allocation and Po<strong>in</strong>ters<br />

NEW TERM<br />

In a <strong>C++</strong> program, memory is allocated dynamically by us<strong>in</strong>g the new operator.<br />

I’m go<strong>in</strong>g to talk about new a little later <strong>in</strong> the chapter, but you need a little sampler as I<br />

cont<strong>in</strong>ue the discussion about po<strong>in</strong>ters. Earlier I talked about structures and used the<br />

mail<strong>in</strong>gListRecord structure as an example. Allocat<strong>in</strong>g a structure from the stack looks like<br />

this:<br />

mail<strong>in</strong>gListRecord listArray;<br />

strcpy(listArray.firstName, “Ian”);<br />

strcpy(listArray.lastName, “Spencer”);<br />

// etc.<br />

That’s what I did earlier when I talked about structures. Now I’ll create the array dynamically<br />

rather than locally:<br />

mail<strong>in</strong>gListRecord* listArray;<br />

listArray = new mail<strong>in</strong>gListRecord;<br />

strcpy(listArray->firstName, “Ian”);<br />

strcpy(listArray->lastName, “Spencer”);<br />

// etc.<br />

The first l<strong>in</strong>e declares a po<strong>in</strong>ter to a mail<strong>in</strong>gListRecord structure. The next l<strong>in</strong>e <strong>in</strong>itializes the<br />

po<strong>in</strong>ter by creat<strong>in</strong>g a new <strong>in</strong>stance of a mail<strong>in</strong>gListRecord structure dynamically. This is the<br />

process by which you dynamically create and access objects <strong>in</strong> <strong>C++</strong>.<br />

And Now Back to Our Program<br />

Now you beg<strong>in</strong> to see where po<strong>in</strong>ters fit <strong>in</strong>to the scheme of th<strong>in</strong>gs. When you create an object<br />

dynamically, the new operator returns a po<strong>in</strong>ter to the object <strong>in</strong> memory. You need that<br />

po<strong>in</strong>ter to be able to do anyth<strong>in</strong>g with the object. Figure 3.1 illustrates how the po<strong>in</strong>ter po<strong>in</strong>ts<br />

to the object <strong>in</strong> memory. Note that although the memory for the dynamically created object<br />

is allocated from heap memory, the actual po<strong>in</strong>ter is a local variable and is allocated from the<br />

stack.

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

Saved successfully!

Ooh no, something went wrong!