AKMA's Random Thoughts

December 04, 2003

Frenzy

Good developments: Trevor and I recruited Micah to help us out with the Disseminary planning and execution. This is a very good step, except that first thing this morning Micah sent me an email reminding me of about eight to-do items. Thanks a lot, fella — there goes your Christmas bonus.

My involvement with the Via Media project and the sermon for Christ Church are developing nicely, and the leadership at St. Luke’s is working up a plan for parish structure. Today’s tutorial on Matthew’s Gospel involved several good discussions; the Early Church History class for today made a congratulations card for me (they are so sweet — really, these folks have only known me a few weeks, and are taking a course outside my area of expertise, and they’ve been warm and appreciative all term).

But meetings and obligations are still flying at me in a way that reminds me of those old hand-drawn Popeye cartoons, where someone digs through a pile of junk, throwing it backward, and Popeye has to dodge the flying debris. Except I have to catch it (all).

Posted by AKMA at December 4, 2003 10:32 PM | TrackBack
Comments

Let's take a moment to reexamine that. What we've done here is create two variables. The first variable is in the Heap, and we're storing data in it. That's the obvious one. But the second variable is a pointer to the first one, and it exists on the Stack. This variable is the one that's really called favoriteNumber, and it's the one we're working with. It is important to remember that there are now two parts to our simple variable, one of which exists in each world. This kind of division is common is C, but omnipresent in Cocoa. When you start making objects, Cocoa makes them all in the Heap because the Stack isn't big enough to hold them. In Cocoa, you deal with objects through pointers everywhere and are actually forbidden from dealing with them directly.

Posted by: Noe at January 13, 2004 03:02 AM

But variables get one benefit people do not

Posted by: Gentile at January 13, 2004 03:02 AM

Note first that favoriteNumbers type changed. Instead of our familiar int, we're now using int*. The asterisk here is an operator, which is often called the "star operator". You will remember that we also use an asterisk as a sign for multiplication. The positioning of the asterisk changes its meaning. This operator effectively means "this is a pointer". Here it says that favoriteNumber will be not an int but a pointer to an int. And instead of simply going on to say what we're putting in that int, we have to take an extra step and create the space, which is what does. This function takes an argument that specifies how much space you need and then returns a pointer to that space. We've passed it the result of another function, , which we pass int, a type. In reality, is a macro, but for now we don't have to care: all we need to know is that it tells us the size of whatever we gave it, in this case an int. So when is done, it gives us an address in the heap where we can put an integer. It is important to remember that the data is stored in the heap, while the address of that data is stored in a pointer on the stack.

Posted by: Marmaduke at January 13, 2004 03:02 AM

For this program, it was a bit of overkill. It's a lot of overkill, actually. There's usually no need to store integers in the Heap, unless you're making a whole lot of them. But even in this simpler form, it gives us a little bit more flexibility than we had before, in that we can create and destroy variables as we need, without having to worry about the Stack. It also demonstrates a new variable type, the pointer, which you will use extensively throughout your programming. And it is a pattern that is ubiquitous in Cocoa, so it is a pattern you will need to understand, even though Cocoa makes it much more transparent than it is here.

Posted by: Isabella at January 13, 2004 10:08 AM

Our next line looks familiar, except it starts with an asterisk. Again, we're using the star operator, and noting that this variable we're working with is a pointer. If we didn't, the computer would try to put the results of the right hand side of this statement (which evaluates to 6) into the pointer, overriding the value we need in the pointer, which is an address. This way, the computer knows to put the data not in the pointer, but into the place the pointer points to, which is in the Heap. So after this line, our int is living happily in the Heap, storing a value of 6, and our pointer tells us where that data is living.

Posted by: Oliver at January 13, 2004 10:11 AM