O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.1. Memory Management in Carbon

Carbon programs generally use C functions to create and destroy memory. You learned about the malloc and free memory functions in Chapter 6, and Carbon programs can certainly make use of these functions. Carbon provides its own memory-management functions, mostly for historical reasons. CoreFoundation also defines its own memory-management conventions, in part because its objects are reference counted. The following sections describe Carbon and CoreFoundation memory-management functions in more detail.

9.1.1. Carbon Memory Functions

Carbon provides its own functions for allocating memory. Although these functions used to be the native memory functions for the traditional Mac OS 9 operating system, they now eventually call down into the malloc memory functions. The Carbon functions exist now largely for source compatibility with existing code. In addition, the terminology for working with memory has made its way into other parts of the Carbon API. Understanding Carbon memory conventions can help you understand how other older libraries, such as QuickTime, are meant to be used.

9.1.1.1. Carbon Pointers

Carbon manages contiguous chunks of memory using the NewPtr and DisposePtr functions. These functions behave basically the same way as malloc and free. The NewPtr function returns a pointer to memory of a specific size, and DisposePtr allows the system to reclaim that memory. You will see the verbs New and Dispose used frequently within Carbon for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required