11
The Heap

So far, your programs have only used memory that has been in frames on the stack. This memory is automatically allocated when the function starts and automatically destroyed when the function ends. (In fact, local variables are often called automatic variables because of this convenient behavior.)

Sometimes, however, you need to explicitly claim a long line of bytes of memory and use it in many functions. For example, you might read a file of text into memory and then call a function that would count all the vowels in that memory. Typically, once you were finished with the text, you would let the program know you were all done so the program could reuse that memory for something else.

Programmers often use the word buffer to mean a long line of bytes of memory. (This explains the term buffering to describe the wait for YouTube to send you enough bytes for that cat video to get started.)

You claim a buffer of memory using the function malloc(). The buffer comes from a region of memory known as the heap, which is separate from the stack. When you’re done using the buffer, you call the function free() to release your claim on that memory and return it to the heap. Let’s say, for example, I needed a chunk of memory big enough to hold 1,000 floats.

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​h​>​ ​/​/​ ​m​a​l​l​o​c​ ​a​n​d​ ​f​r​e​e​ ​a​r​e​ ​i​n​ ​s​t​d​l​i​b​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​/​/​ ​D​e​c​l​a​r​e​ ​a​ ​p​o​i​n​t​e​r​
 ​ ​ ​ ​f​l​o​a​t​ ​*​s​t​a​r​t​O​f​B​u​f​f​e​r​;​

 ​ ​ ​ ​/​/​ ​A​s​k​ ​t​o​ ​u​s​e​ ​s​o​m​e​ ​b​y​t​e​s​ ​f​r​o​m​ ​t​h​e​ ​h​e​a​p​
 ​ ​ ​ ​s​t​a​r​t​O​f​B​u​f​f​e​r​ ​=​ ​m​a​l​l​o​c​(​1​0​0​0​ ​*​ ​s​i​z​e​o​f​(​f​l​o​a​t​)​)​;​

 ​ ​ ​ ​/​/​ ​.​.​.​u​s​e​ ​t​h​e​ ​b​u​f​f​e​r​ ​h​e​r​e​.​.​.​

 ​ ​ ​ ​/​/​ ​R​e​l​i​n​q​u​i​s​h​ ​y​o​u​r​ ​c​l​a​i​m​ ​o​n​ ​t​h​e​ ​m​e​m​o​r​y​ ​s​o​ ​i​t​ ​c​a​n​ ​b​e​ ​r​e​u​s​e​d​
 ​ ​ ​ ​f​r​e​e​(​s​t​a​r​t​O​f​B​u​f​f​e​r​)​;​

 ​ ​ ​ ​/​/​ ​F​o​r​g​e​t​ ​w​h​e​r​e​ ​t​h​a​t​ ​m​e​m​o​r​y​ ​i​s​
 ​ ​ ​ ​s​t​a​r​t​O​f​B​u​f​f​e​r​ ​=​ ​N​U​L​L​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

startOfBuffer would be a pointer to the first floating point number in the buffer.

Figure 11.1  A pointer on the stack to a buffer on the heap

A pointer on the stack to a buffer on the heap

At this point, most C books would spend a lot of time talking about how to read and write data in assorted locations in that buffer of floating pointer numbers. This book, however, is trying to get you to objects as quickly as possible. So, we will put off C arrays and pointer arithmetic until later.

You can also use malloc() to claim space for a struct on the heap. For example, if you wanted to allocate a Person struct on the heap, you might have a program like this:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​h​>​

t​y​p​e​d​e​f​ ​s​t​r​u​c​t​ ​{​
 ​ ​ ​ ​f​l​o​a​t​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;​
 ​ ​ ​ ​i​n​t​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​;​
}​ ​P​e​r​s​o​n​;​

f​l​o​a​t​ ​b​o​d​y​M​a​s​s​I​n​d​e​x​(​P​e​r​s​o​n​ ​*​p​)​
{​
 ​ ​r​e​t​u​r​n​ ​p​-​>​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​/​ ​(​p​-​>​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​*​ ​p​-​>​h​e​i​g​h​t​I​n​M​e​t​e​r​s​)​;​
}​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​/​/​ ​A​l​l​o​c​a​t​e​ ​m​e​m​o​r​y​ ​f​o​r​ ​o​n​e​ ​P​e​r​s​o​n​ ​s​t​r​u​c​t​u​r​e​
 ​ ​ ​ ​P​e​r​s​o​n​ ​*​x​ ​=​ ​(​P​e​r​s​o​n​ ​*​)​m​a​l​l​o​c​(​s​i​z​e​o​f​(​P​e​r​s​o​n​)​)​;​

 ​ ​ ​ ​/​/​ ​F​i​l​l​ ​i​n​ ​t​w​o​ ​m​e​m​b​e​r​s​ ​o​f​ ​t​h​e​ ​s​t​r​u​c​t​u​r​e​
 ​ ​ ​ ​x​-​>​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​=​ ​8​1​;​
 ​ ​ ​ ​x​-​>​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​=​ ​2​.​0​;​

 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​o​u​t​ ​t​h​e​ ​B​M​I​ ​o​f​ ​t​h​e​ ​o​r​i​g​i​n​a​l​ ​P​e​r​s​o​n​
 ​ ​ ​ ​f​l​o​a​t​ ​x​B​M​I​ ​=​ ​b​o​d​y​M​a​s​s​I​n​d​e​x​(​x​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​x​ ​h​a​s​ ​a​ ​B​M​I​ ​o​f​ ​=​ ​%​f​​n​"​,​ ​x​B​M​I​)​;​

 ​ ​ ​ ​/​/​ ​L​e​t​ ​t​h​e​ ​m​e​m​o​r​y​ ​b​e​ ​r​e​c​y​c​l​e​d​
 ​ ​ ​ ​f​r​e​e​(​x​)​;​

 ​ ​ ​ ​/​/​ ​F​o​r​g​e​t​ ​w​h​e​r​e​ ​i​t​ ​w​a​s​
 ​ ​ ​ ​x​ ​=​ ​N​U​L​L​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Notice the operator ->. p->weightInKilos says, Dereference the pointer p to the structure and get me the member called weightInKilos.

This idea of structures on the heap is a very powerful one. It forms the basis for Objective-C objects, which we turn to next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset