Sneak Peek: Reference vs. Pointer

It’s time to let the cat out of the bag.

I’m writing a book that explains iOS programming to .NET developers! I have written seven of the twelve chapters and, I must say, it’s really coming together nicely so far. This book will be a companion manual for any .NET developer looking to make the leap into iOS programming. Considering the explosive growth in demand for iOS developers these days, I think a lot of people might benefit from a book like this.

I’m writing the book I wished for when I started teaching myself native iOS programming. It explains the terms and concepts involved with iOS programming by relating them to your knowledge of .NET development.

Here is a sneak peek from Chapter 4, titled ‘From C# to Objective-C’…


Reference vs. Pointer

Every object in a program exists at a unique memory address. Most of the time C# code stores the location of an object as a variable in the form of a reference. In contrast, Objective-C stores the memory address as a variable in the form of a pointer. C# also supports pointers, but only in blocks of code explicitly marked as ‘unsafe.’ Objective-C only supports pointers.

In iOS software every pointer consumes four bytes of memory, according to the sizeof() operator. Those bytes store a number. That number happens to be a memory address of either zero (which is known as nil) or an address at which a data object should exist. Note that I did not say a data object must exist at the address stored in a non-nil pointer. This is the fundamental difference between pointers and references. Pointers are more flexible than references, but less safe.

Another way to think about the difference between references and pointers is to consider how they are used. In .NET programming there is a garbage collector (GC) that removes unreferenced objects from memory. The GC also moves objects around on the heap (the section of memory where objects are allocated) in order to create contiguous blocks of available memory, making subsequent allocations faster. The fact that objects are moving around on the heap is completely irrelevant to C# code that only uses references to access those objects. The .NET runtime takes care of adjusting references to ensure they point to relocated objects. A reference points to an object, regardless of where it currently happens to be in memory.

Pointers, on the other hand, point to a specific location in an application’s memory address space. If an object is moved, pointers to the object’s original memory address are not automatically updated with the new address. However, since the Objective-C runtime on iOS does not have a garbage collector, and objects do not move around in memory on their own, this is not as much of a problem as one might think. The additional burden imposed by pointers on application developers is partially mitigated by Automatic Reference Counting, which I review in Chapter 5. Also, what is lost in safety and convenience by not having references is compensated by the flexibility of using pointers.


Stay tuned for more…

[UPDATE]

The book has been published! Please visit this page for more information.

This entry was posted in iOS Programming for .NET Developers, Objective-C. Bookmark the permalink.

47 Responses to Sneak Peek: Reference vs. Pointer

  1. Pingback: Big announcement on my iOS blog « Josh Smith on WPF

  2. mleyzaola says:

    I look forward for that book to be on the stores :p
    Next month I am going to work on iOS project and have no clue about the subject (yes, I can feel the pain already). Not even a bloody hello world with Monotouch.

    Do you have any sort of schedule for finishing your book Josh?

    Thanks

    • Josh Smith says:

      @mleyzaola – I plan on making the book available in iBooks, Kindle, Nook, and paperback by early fall. I’m hoping for sometime in September, if life doesn’t get in the way.

    • Josh Smith says:

      I am happy to let you know that my book about iOS programming is now available! For more info about the book, visit http://iosfordotnetdevs.com/

  3. Great news! It should be a very interesting read. Although I will never leave monotouch, if I read one objective-c book … it would be yours 🙂

  4. That’s a really neat idea. Having just entered the Apple world, I’m itching to develop for these shiny devices! You’ve got a sale here 😉

    You’ve got a sale here 😉

  5. Ben Smith says:

    Hi Josh,

    Do you have any plans to release an early access ‘beta’ version of the book before launch? I’d be interested in this and the book once finished.

    Regards,
    Ben

  6. jerryhill says:

    Josh,
    As a WPF lover and a Josh Smith follower for the past 4 years and MFC/C++ developer for 20+ – I found myself prepared to start iPhone development – so 4 years ago I started and have found it to be enjoyable and the MVVM/MVC pattern flows together in the development so nicely it all seems very similar now-in fact, Objective C lets me believe I am in charge vs. the .NET framework – Really hoped the .NET community would have jumped on XAML/WPF more but I am amazed how many developers refuse to dive deep (i.e. know why you need DataContextSpy for starters) and many of us did – thanks to your work and hoping that your book helps them make the jump to this mini-computer via Objective C – looking forward to it and the .NET community needs a solid cross reference spelled out.

    Thanks.

  7. Joe White says:

    “In iOS software every pointer consumes four bytes of memory”

    Really? So Macs don’t support 64-bit apps?

  8. Josh – couldn’t come at a better time. Really look forward to it!

    • Josh Smith says:

      Thanks Jason! I am burning the candle at both ends trying to get the book finished. It’s a huge amount of work, and I insist that the quality be as high as I can make it. If all goes as planned, the book will launch in early September…

    • Josh Smith says:

      Hi Jason, I am happy to let you know that my book about iOS programming is now available! For more info about the book, visit http://iosfordotnetdevs.com/

  9. Jordan says:

    Can’t Wait!

  10. Bobby Johnson says:

    Nice! Please, please, please put in detailed chapters on unit testing.

  11. db7uk says:

    I too have followed your moves through WPF. Your book “Advanced MVVM” was great and really set me off on the path of WPF!. I still work with WPF and Silverlight but eagerly await your next book as IOS is very unknown to me….. for the time being. Thank you for your work and the inspiration you give me.

  12. I’ll be buying a copy.

    Perhaps some of your WPF & WCF colleagues can be persuaded to include suggestions for other topics of interest such as data access solutions for iOS apps.

    • Josh Smith says:

      I’m definitely open to suggestions for topics, though keep in mind that the overall design of the book’s content has already been determined. Making changes is not always easy, because the structure and flow of the book are very important to me. But, regarding data access solutions…that’s already a major component of Part III. 😉

    • Josh Smith says:

      Mark,
      I am happy to let you know that my book about iOS programming is now available! For more info about the book, visit http://iosfordotnetdevs.com/

  13. Georges Landet says:

    WPF/WCF dev here. I’m not planning to use iOS soon, but I’m really interested in your (huge, no doubt) work on this subject. I’ve been following you through all these WPF years with excitement, I love WPF/MVVM. I hope to find that excitement again while reading your book. The iOS beast seems far too scary right now for me to dive into it. Thanks for this announcement.

    • Josh Smith says:

      That’s really cool, Georges. Reading about iOS just for the sake of learning something new, I respect that. I look forward to making this book available for you!

    • Josh Smith says:

      Hello Georges, I am happy to let you know that my book about iOS programming is now available! For more info about the book, visit http://iosfordotnetdevs.com/

  14. dodgy_coder says:

    Looking forward to this, I’ve been developing in WPF & MVVM for couple of years time now and have been putting off learning Objective-C. I have an idea for an app I’ve mocked up already and I’m looking to develop it on iOS. One question I had is how much of iOS UI concepts are you covering as opposed to say pure Objective-C language topics?

    • Josh Smith says:

      I am covering a wide spectrum of topics, including pure Obj-C language topics, the UI frameworks, tools for creating UIs, using the core classes that in .NET are part of the Base Class Library, and more. I cover high-level conceptual topics and practical hands-on information, always relating things back to .NET programming, if possible. The scope of the book is quite large.

    • Josh Smith says:

      Hi dodgy_coder, I am happy to let you know that my book about iOS programming is now available! For more info about the book, visit http://iosfordotnetdevs.com/

  15. terroare says:

    How come that C and C++ have both, pointers and references, and Objective-C has only pointers? Is there a reason for this ?

    • Josh Smith says:

      C does not have references. C++ supports something known as references, but they are very different from .NET references. For example, they cannot be reassigned. I don’t know why the designers of Objective-C did not include something like C++ references, but I don’t think it’s a shortcoming of the language.

  16. totally think this is an awesome idea for a book, especially if it’s written in a way to “transition” across to iOS. I would definitely buy this book – is there a mailing list or anything we can register for updates?? Thanks for the effort you’ve put into doing this Josh!

  17. Chris Horn says:

    This is Gold Josh, I’ve been meaning to make the jump for a while, I acquired an iMac about 18 months to 2 years ago and have slow been collecting IOS devices over time, so I’m not sure whats been stopping me. Mac OS is a welcome friend at the end of a long day as .Net developer, and If I need to get my visual Studio on at home I just fire up Parallels. This is a book I’ll be buying. Keep up the good work.

  18. aeontimeline says:

    Hi Josh,
    I am actually trying to make the jump the other way, and go from writing with Objective-C/Cocoa for MacOS back to the Windows environment.

    I have written about the difficulties I have run into here: http://wp.me/pkr2v-6P

    Given your insight into both sides of the fence, I would appreciate your insight into what I should do to satisfy those asking for a Windows version of my Application: Win Forms, WPF, WinRT… What is the best framework to get this kind of job done? And what will require a re-write in 3-5 years time?

    I would have posted in your WinRt post that I linked to from mine, but the comments were closed.

    I seem to be the only developer going the other way at the moment, from Apple back to Windows, but I thought it still seems relevant to your post. Feel free to delete this comment if you feel it strays too far from your original topic. It is really just an appeal for help.

    Thanks,
    Matt

    • Josh Smith says:

      I replied on your blog post. Sorry, there is no hidden truth to share. You nailed it.

Comments are closed.