Jump to content

How to Work with Pointers and Memory When Using the iOS 4 SDK

0
  chco's Photo
Posted Feb 20 2011 06:04 AM

The excerpt below from Learning the iOS 4 SDK for Javascript Programmers explains the concept of pointers and how to use them properly when programming iOS4. This will be especially helpful for Javascript programmers since Javascript, like most interpreted languages, does not use pointers.
To begin the exploration of pointers, it helps to have a visualization of what computer memory is and how programs use it. Although there are many types of memory, the kind we are concerned with is traditionally called random access memory, or RAM for short. Some types of RAM preserve their contents when not in use, while others empty everything when power is removed. For our discussion, RAM is RAM, regardless of what happens to it when the lights go out.

Memory is divided into tiny spaces. Imagine a gigantic cabinet with gazillions of identically sized cubbyholes in it. Each cubbyhole can hold one small chunk of information. While a program runs, it uses the cubbyholes to temporarily store information. Various kinds of information require differently sized blocks of cubbyholes to hold their data—a short string of text characters uses very little space, while a two-megapixel image occupies a lot of cubbyholes, as represented below.

A representation of memory locations and differently sized blocks occupied by data

Attached Image


Keeping track of where a new chunk of information goes is one of the jobs of the central processing unit, or CPU, of the device. The program (deep, deep down and with the help of the operating system) asks the CPU for a place to store information of a particular size; the CPU locates an unused block of cubbies, and stores the data in those cubbies. Of course, the program will likely need to retrieve and/or change that information over time, so the program needs to know where to get it (or, more precisely, where to tell the CPU to get it).

Helping this system along is a convention that assigns a numeric address to each cubbyhole. The numbers are all sequential, so if a 50-chunk block of data is stored in cubbyholes starting at number 100, the block of data occupies memory addresses 100 through 149. When the program declares that it’s going to store data, the CPU not only locates empty space and moves the data into those cubbyholes, but the CPU also reports back to the program the address of the first cubbyhole. That address—a numeric value—is the pointer to the data. Later, when the program wants to pass the data to another function or object, it doesn’t have to move or copy all of that data from memory to transfer it to another operation. Instead, the program passes only the pointer so that the other operation can use the one perfect copy of the data that exists in memory, demonstrated in the figure below. The actual image, myImage, is at location 2,419,920. Another word of memory holds the value 2,419,920, and this word is labeled *myImage (with the preceding star) because the program is defining a pointer here.

A pointer occupies little memory space, but can point to the start of a much larger block of data

Attached Image


By passing along only the comparatively small pointer, the program uses far fewer resources in the process. Doing so also ensures the integrity of the data in memory. If the one “master” copy in memory should change between the time it is passed to another operation and the time the other operation acts on the data, the other operation will be working on the most up-to-date version.

Allow me to use one more metaphor—this time from the Web world—to help convey how a pointer can be a good thing. Let’s say you create a sophisticated web page with lots of images, scripts, stylesheets, and Ajax-delivered content. You now want to share this page with several of your friends. You could save the page from your browser (with copies of all its ancillary files, as many browsers let you do these days) and email the entire package to each of your friends. Each email message would contain multiple megabytes of data representing the snapshot of the live page. Or you could send the URL—the address—of the page in an email message. As each recipient opens the message, he or she plugs that address into a browser to view the page as it exists on the server at that instant. You’ve accomplished two things. First, you saved email bandwidth by transmitting less than one hundred bytes instead of megabytes. Second, each recipient will be able to view the absolute latest version of the page. The URL acted as a pointer.

Cover of Learning the iOS 4 SDK for Javascript Programmers
Learn more about this topic from Learning the iOS 4 SDK for Javascript Programmers. 

Is it possible for Javascript programmers to learn the iPhone SDK and live to tell the tale? Technology guru Danny Goodman did, and in this book he leaves a well-marked trail for you to follow. Goodman understands the challenges you might face with the SDK, and he introduces Xcode, Objective-C, and Cocoa Touch in a context you'll readily understand. If you're a Javascript programmer and want to take advantage of the iPhone and iPad, iOS 4 SDK is your tool -- and this is your book. Includes full coverage of iOS SDK 4.2.

Learn More Read Now on Safari


1 Reply

 : Feb 21 2011 12:03 PM
That is an interesting article. I have to put it to practice to check that out, because before I was very confused about it.