Foundations of Programming - Karl Seguin
Foundations of Programming - Karl Seguin
Foundations of Programming - Karl Seguin
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 7 - Back to Basics: Memory<br />
Some prefer to call the type <strong>of</strong><br />
pointers found in C#, VB.NET and<br />
Java References or Safe Pointers,<br />
because they either point to a valid<br />
object or null. There is no such<br />
guarantee in languages such as C<br />
or C++ since developers are free to<br />
manipulate pointers directly.<br />
Interestingly, when we leave our method, the boss variable<br />
will pop <strong>of</strong>f the stack, but the subordinate, which is defined in<br />
a parent scope, won't. This means the garbage collector won't<br />
have anything to clean-up because both heap values will still<br />
be referenced (one directly from the stack, and the other<br />
indirectly from the stack through a referenced object).<br />
As you can see, pointers most definitely play a significant part<br />
in both C# and VB.NET. Since pointer arithmetic isn't available<br />
in either language, pointers are greatly simplified and<br />
hopefully easily understood.<br />
Memory Model in Practice<br />
We'll now look at the actual impact this has on our applications. Keep in mind though that<br />
understanding the memory model in play won't only help you avoid pitfalls, but it will also help you<br />
write better applications.<br />
Boxing<br />
Boxing occurs when a value type (stored on the stack) is coerced onto the heap. Unboxing happens<br />
when these value types are placed back onto the stack. The simplest way to coerce a value type, such as<br />
an integer, onto the heap is by casting it:<br />
int x = 5;<br />
object y = x;<br />
A more common scenario where boxing occurs is when you supply a value type to a method that<br />
accepts an object. This was common with collections in .NET 1.x before the introduction <strong>of</strong> generics. The<br />
non-generic collection classes mostly work with the object type, so the following code results in boxing<br />
and unboxing:<br />
ArrayList userIds = new ArrayList(2);<br />
userIds.Add(1);<br />
userIds.Add(2);;<br />
int firstId = (int)userIds[0];<br />
The real benefit <strong>of</strong> generics is the increase in type-safety, but they also address the performance penalty<br />
associated with boxing. In most cases you wouldn't notice this penalty, but in some situations, such as<br />
large collections, you very well could. Regardless <strong>of</strong> whether or not it's something you ought to actually<br />
concern yourself with, boxing is a prime example <strong>of</strong> how the underlying memory system can have an<br />
impact on your application.<br />
<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />
57