<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Umair&#039;s Blog</title>
	<atom:link href="http://umairsaeed.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://umairsaeed.com</link>
	<description></description>
	<lastBuildDate>Fri, 10 May 2013 13:27:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='umairsaeed.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Umair&#039;s Blog</title>
		<link>http://umairsaeed.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://umairsaeed.com/osd.xml" title="Umair&#039;s Blog" />
	<atom:link rel='hub' href='http://umairsaeed.com/?pushpress=hub'/>
		<item>
		<title>A Quine in Objective-C</title>
		<link>http://umairsaeed.com/2012/07/18/a-quine-in-objective-c/</link>
		<comments>http://umairsaeed.com/2012/07/18/a-quine-in-objective-c/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 13:46:06 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=199</guid>
		<description><![CDATA[A quine is a program that takes no input and outputs its own source code. It has been a while since I last wrote a quine, so I figured I&#8217;ll write one in Objective-C. In general, quines follow a fairly &#8230; <a href="http://umairsaeed.com/2012/07/18/a-quine-in-objective-c/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=199&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A quine is a program that takes no input and outputs its own source code. It has been a while since I last wrote a quine, so I figured I&#8217;ll write one in Objective-C. In general, quines follow a fairly simple formula. The program contains a string that includes all the code before the string and all the code after the string. Depending on the programming language, the string might also contain format string (for languages that use format-strings to print to stdout)</p>
<p>Here is the code for the quine. I&#8217;ve broken the code below into segments to make it simpler to understand:</p>
<pre>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt;  
int main (int argc, const char * argv[]) { 
    @autoreleasepool 
    { 
        NSString *str=@&quot; \
            #import &lt;Foundation/Foundation.h&gt; %c \
            int main (int argc, const char * argv[]) { @autoreleasepool { NSString *str= \
            %c%c%@%c; \
            NSLog(str, 10, 64, 34, str, 34);} return 0;}&quot;;
        NSLog(str, 10, 64, 34, str, 34); 
    } 
    return 0;
}
</pre>
<p>Before I go on, 10, 64 and 34 are the ASCII character codes for newline, @ and " respectively. To make it clearer, I've broken the string into four lines. The first two lines contain the source code before the string. The 3rd line is the format string and the last line is the source code after the string. Here's the code with all lines collapsed:</p>
<pre>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt;  
int main (int argc, const char * argv[]) { 
    @autoreleasepool 
    { 
        NSString *str=@&quot;#import &lt;Foundation/Foundation.h&gt; %c int main (int argc, const char * argv[]) { @autoreleasepool { NSString *str=%c%c%@%c; NSLog(str, 10, 64, 34, str, 34);} return 0;}&quot;;
        NSLog(str, 10, 64, 34, str, 34); 
    } 
    return 0;
}
</pre>
<p>Finally, here's the output when you execute this program. As you can tell, this is the same as the original program (minus the whitespace)</p>
<pre>
<pre class="brush: objc; title: ; notranslate">
#import &lt;Foundation/Foundation.h&gt; 
 int main (int argc, const char * argv[]) { @autoreleasepool { NSString *str=@&quot;#import &lt;Foundation/Foundation.h&gt; %c int main (int argc, const char * argv[]) { @autoreleasepool { NSString *str=%c%c%@%c; NSLog(str, 10, 64, 34, str, 34);} return 0;}&quot;; NSLog(str, 10, 64, 34, str, 34);} return 0;}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/199/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=199&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2012/07/18/a-quine-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>
	</item>
		<item>
		<title>Compiling Objective-C via Command Line</title>
		<link>http://umairsaeed.com/2012/07/15/compiling-objective-c-via-command-line/</link>
		<comments>http://umairsaeed.com/2012/07/15/compiling-objective-c-via-command-line/#comments</comments>
		<pubDate>Sun, 15 Jul 2012 13:34:05 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=196</guid>
		<description><![CDATA[If you want to do any kind of programming for iOS or Mac OS X, you really have to use Xcode and the sooner you become comfortable with it, the better off you will be. However, there are times when &#8230; <a href="http://umairsaeed.com/2012/07/15/compiling-objective-c-via-command-line/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=196&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you want to do any kind of programming for iOS or Mac OS X, you really have to use Xcode and the sooner you become comfortable with it, the better off you will be. However, there are times when I want to write a very simple (and mostly throwaway) command-line program. For such single-file programs, occasionally I find it preferable to simply use the LLVM Clang Objective-C compiler via the command line, as it saves me from creating an entire Xcode project.</p>
<p>Here is the general format of the clang command that I use most commonly:</p>
<p><strong>clang -fobjc-arc –framework Foundation files -o outputName</strong></p>
<p>The <strong>-framework</strong> option tells the compiler about the frameworks to use. If, like me, you use Cocoa, you will want to include the Foundation framework.</p>
<p>The <strong>-fobjc-arc</strong> option tells the compiler to use ARC (Automatic Reference Counting). Skipping this option means that I will need to take care of memory management.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/196/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/196/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=196&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2012/07/15/compiling-objective-c-via-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>
	</item>
		<item>
		<title>Finding the Start of a Loop in a Circular Linked List</title>
		<link>http://umairsaeed.com/2011/06/23/finding-the-start-of-a-loop-in-a-circular-linked-list/</link>
		<comments>http://umairsaeed.com/2011/06/23/finding-the-start-of-a-loop-in-a-circular-linked-list/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 13:54:07 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Algos]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=186</guid>
		<description><![CDATA[A lot of people are familiar with the problem of detecting a loop in a linked list. The problem goes as follows: “Given a linked list, what is the algorithm to determine if it has any cycles (loops)?” The algorithm &#8230; <a href="http://umairsaeed.com/2011/06/23/finding-the-start-of-a-loop-in-a-circular-linked-list/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=186&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A lot of people are familiar with the problem of detecting a loop in a linked list. The problem goes as follows: “Given a linked list, what is the algorithm to determine if it has any cycles (loops)?”</p>
<p>The algorithm is pretty straightforward:</p>
<ol>
<li>We start at the beginning of the linked list with two pointers.</li>
<li>The first pointer is incremented through each node of the list. The second pointer moves twice as fast, and skips every other node.</li>
<li>If the linked list contains a loop, these two pointers will eventually meet at the same node, thus indicating that the linked list contains a loop.</li>
</ol>
<p>The algorithm is straightforward and it is relatively easy to create a mental model and get an intuitive sense of why it works.</p>
<p>Now, a slight twist to the same question asks: “Given a circular linked list, what is the algorithm to find the first node of the loop.” For instance, in the circular list A-&gt;B-&gt;C-&gt;D-&gt;E-&gt;C, the first node of the loop is node C. The first part of the algorithm is identical to the algorithm for finding if there is a loop (above). Once a loop has been found, the following additional steps will give us the starting node of the loop:</p>
<ol>
<li>Once a loop as been detected (step-3 above), move one of the pointers to the beginning (head) of the linked list. The second pointer remains where it was at the end of step-3.</li>
<li>Increment both pointers one node at a time. The node at which the two pointers meet will be the starting node of the loop!</li>
</ol>
<p>This algorithm isn’t too difficult compared to the algorithm for detecting a loop. However, the mental model seems a bit trickier. Why and how does it always find the start of the loop?</p>
<p><strong><br />
</strong></p>
<h3>How does the Algorithm work? An intuitive explanation:</h3>
<p>Here’s some explanation which would hopefully help you intuitively understand why the algorithm works, without going into a lot of mathematical detail.</p>
<p><strong>First, meeting point of two pointers in a loop</strong></p>
<p>Consider two pointers: a slow pointer S that increments by one node at each step, and a fast pointer F that increments by two nodes at each step (i.e. it is twice as fast as S). Both pointers start at the same time from the beginning of an n-node loop. In the time S covers n nodes. F will have covered 2n nodes and they will both meet at the start of the loop.</p>
<p>Now, let us say that the slow pointer S starts at the beginning of the loop, and the fast pointer F starts at node k (where k &lt; n) of the loop. As these two pointers move along the loop, they will meet at node (n-x).</p>
<p>What we really need to do is figure out x, as it will give us the node at which the two pointers meet inside the loop.</p>
<ol>
<li>When S takes n/2 steps, it will be at node n/2. During the same time, F will have taken 2(n/2) = n steps, and it will be at node (k+n). Since the we are inside a loop, F will be effectively back at node k.</li>
<li>In order for the two pointers to meet at node (n-x), S needs to take a further (n-x-n/2)=(n/2-x) steps and it will end up at node n-x. During the same time, F will have taken 2*(n/2-x)=(n-2x) steps and will be at node k+(n-2x). Given our assumption that both S and F meet at the same node:</li>
</ol>
<pre>		n-x = k+n-2x
	=&gt;	  x = k</pre>
<p>This means that if S starts from the start of the loop, and F starts k nodes into the loop, both of them will meet at node (n-k), i.e k nodes from the end of the loop. This is a key insight.</p>
<p><strong>Circular Linked List</strong></p>
<p>Now, coming back to the linked list that contains a loop. Suppose the start of the loop is m (e.g. m=3) nodes from the start of the linked list. Both S and F start at the beginning of the linked list [Figure-1].</p>
<div class="image">
<p><img style="display:block;margin-left:0;margin-right:auto;" title="Figure-1.jpg" src="http://umairsd.files.wordpress.com/2011/06/figure-1.jpg?w=500&#038;h=338" alt="Figure-1: Circular linked list with S and F pointers at the start" width="500" height="338" border="0" /></p>
<div><small>Figure-1: Circular linked list with S and F pointers at the start</small></div>
</div>
<p>&nbsp;</p>
<p>By the time S gets to node m (i.e. start of loop), F will be at node 2m [Figure-2]. This means that S will be at the start of the loop and F will be m nodes <strong>*into the loop*</strong>.</p>
<div class="image">
<p><img style="display:block;margin-left:0;margin-right:auto;" title="Figure-2.jpg" src="http://umairsd.files.wordpress.com/2011/06/figure-2.jpg?w=500&#038;h=345" alt="Figure-2: Circular linked list, with S at the start of loop and F m nodes into the loop" width="500" height="345" border="0" /></p>
<div><small>Figure-2: Circular linked list, with S at the start of loop and F m nodes into the loop</small></div>
</div>
<p>&nbsp;</p>
<p>Based on the discussion above, we already know that if S begins from the start of the loop and F starts from node m, they will meet m nodes from the end of the loop (i.e. the orange-node in [Figure-3]).</p>
<div class="image">
<p><img style="display:block;margin-left:0;margin-right:auto;" title="Figure-3.jpg" src="http://umairsd.files.wordpress.com/2011/06/figure-3.jpg?w=500&#038;h=317" alt="Figure-3: Both F and S meet m nodes from the end of the loop" width="500" height="317" border="0" /></p>
<div><small>Figure-3: Both F and S meet m nodes from the end of the loop</small></div>
</div>
<p>&nbsp;</p>
<p>At this point, keep the pointer F at the orange-node where the two pointers met (i.e. m-nodes from the start of the loop), and move the pointer S to the beginning of the linked list [Figure-4]. Now, if we increment both S and F *one node at a time*, it is obvious that they will meet at &#8216;Node-m&#8217; (red-node) of the list, which is the start of the loop.</p>
<div class="image">
<p><img style="display:block;margin-left:0;margin-right:auto;" title="Figure-4.jpg" src="http://umairsd.files.wordpress.com/2011/06/figure-4.jpg?w=500&#038;h=325" alt="Figure-4: S at the start of linked list, F at the point they met. Both increment one at a time from here-on" width="500" height="325" border="0" /></p>
<div><small>Figure-4: S at the start of linked list, F at the point they met. Both increment one at a time from here-on</small></div>
</div>
<p>&nbsp;</p>
<p>For the curious, here’s the Java code snippets for detecting a loop in a linked list and finding the starting node. The complete source code for my linked list project is at my Github page (<a href="https://github.com/umairsd/LinkedList-Java">https://github.com/umairsd/LinkedList-Java</a>):</p>
<pre class="brush: java; title: ; notranslate">
	/**
	 * Checks if the given linked list is a circular linked list (i.e. it
	 * contains a loop). This means a list in which a node's next pointer points
	 * to an earlier node, so as to make a loop in the linked list. For
	 * instance:
	 * 			A -&gt; B -&gt; C -&gt; D -&gt; E -&gt; C
	 *
	 * @param linkedList
	 *            the linked list to be tested
	 * @return true if there is a loop, false if there isn't
	 */
	public static boolean hasLoop(LinkedList linkedList) {
		if (linkedList == null || linkedList.getHead() == null) {
			return false;
		}

		IntegerNode slow = linkedList.getHead();
		IntegerNode fast = linkedList.getHead();

		while (true) {
			slow = slow.getNext();

			if (fast.getNext() != null) {
				fast = fast.getNext().getNext();
			} else {
				return false;
			}

			if (slow == null || fast == null) {
				return false;
			}

			if (slow == fast) {
				return true;
			}
		}
	}
</pre>
<p>&nbsp;</p>
<pre class="brush: java; title: ; notranslate">

	/**
	 * Returns the node at the start of a loop in the given circular linked
	 * list. A circular list is one in which a node's next pointer points
	 * to an earlier node, so as to make a loop in the linked list. For
	 * instance:
	 *
	 * input: A -&gt; B -&gt; C -&gt; D -&gt; E -&gt; C [the same C as earlier]
	 * output: C
	 *
	 * (CCI_0205)
	 *
	 * @param linkedList
	 *            list to be tested
	 * @return the node at the start of the loop if there is a loop, null
	 * otherwise
	 */
	public static IntegerNode findLoopStart(LinkedList linkedList) {
		if (linkedList == null || linkedList.getHead() == null) {
			return null;
		}

		IntegerNode loopStartNode = null;
		IntegerNode slow = linkedList.getHead();
		IntegerNode fast = linkedList.getHead();

		while (slow != null &amp;&amp; fast != null) {
			slow = slow.getNext();
			if (fast.getNext() == null) {
				loopStartNode = null;
				break;
			}
			fast = fast.getNext().getNext();

			// If slow and fast point to the same node, it means that the
			// linkedList contains a loop.
			if (slow == fast) {

				slow = linkedList.getHead();

				while (slow != fast) {
					// Keep incrementing the two pointers until they both
					// meet again. When this happens, both the pointers will
					// point to the beginning of the loop
					slow = slow.getNext(); // Can't be null, as we have a loop
					fast = fast.getNext(); // Can't be null, as we have a loop
				}

				loopStartNode = slow;
				break;
			}
		}

		return loopStartNode;
	}

</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=186&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2011/06/23/finding-the-start-of-a-loop-in-a-circular-linked-list/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/06/figure-1.jpg" medium="image">
			<media:title type="html">Figure-1.jpg</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/06/figure-2.jpg" medium="image">
			<media:title type="html">Figure-2.jpg</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/06/figure-3.jpg" medium="image">
			<media:title type="html">Figure-3.jpg</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/06/figure-4.jpg" medium="image">
			<media:title type="html">Figure-4.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>Incorrect C++ compiler behavior in Visual Studio</title>
		<link>http://umairsaeed.com/2011/01/31/incorrect-c-compiler-behavior-in-visual-studio/</link>
		<comments>http://umairsaeed.com/2011/01/31/incorrect-c-compiler-behavior-in-visual-studio/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 14:36:29 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Compiler]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/2011/01/31/incorrect-c-compiler-behavior-in-visual-studio/</guid>
		<description><![CDATA[[Note: Special credit for this post goes to my colleague Yuliy Gerchikov, who first discovered this issue] As most C++ programmers know, an object that has been constructed via the new expression must be destroyed by calling delete. The delete-expression &#8230; <a href="http://umairsaeed.com/2011/01/31/incorrect-c-compiler-behavior-in-visual-studio/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=172&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>[Note: Special credit for this post goes to my colleague Yuliy Gerchikov, who first discovered this issue]</p>
<p>As most C++ programmers know, an object that has been constructed via the new expression must be destroyed by calling delete. The delete-expression invokes the object&#8217;s destructor which in turn is responsible for clean up tasks. For instance, here&#8217;s some sample code that shows object creation, and then freeing it up via delete:</p>
<pre class="brush: cpp; title: ; notranslate">
	Foo *pF = new Foo();
	// Do some work
	delete pF;
</pre>
<p>The <a href="http://http://www.kuzbass.ru:8086/docs/isocpp/expr.html" target="_blank">C++ Specification</a> for delete has this to say:</p>
<p><em>The delete-expression operator destroys a most derived object (1.8) or array created by a new-expression.</em></p>
<p><em> delete-expression:<br />
::<sub>opt</sub> delete cast-expression<br />
::<sub>opt</sub> delete [ ] cast-expression<br />
</em></p>
<p><em>The first alternative is for non-array objects, and the second is for arrays. The operand shall have a pointer type, or a class type having a single conversion function (12.3.2) to a pointer type. <strong>The result has type void.</strong> (emphasis mine)</em></p>
<p>So, given that the delete expression has the return type void, what do you think the C++ compiler should do when it sees the following code:</p>
<pre class="brush: cpp; title: ; notranslate">
	Foo *pF = new Foo();
	// Do some work
	delete delete pF;
</pre>
<p>But delete returns void, so it is reasonable to assume that the C++ compilers would throw a fit on the above code, right? Right? Unfortunately, that&#8217;s not always the case.</p>
<p>Let&#8217;s look at some C++ code. I have two user defined types Foo and Bar. The difference between the two types is that I declare and define an explicit destructor for Foo.</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
class Foo {
	public: ~Foo() { std::cout&lt;&lt;&quot;In dtor&quot;&lt;&lt;std::endl; }
};

class Bar {};

int main(){
	Foo *pf = new Foo();
    delete delete pf;

    Bar *pb = new Bar();
    delete delete pb;

    return 0;
}
</pre>
<h5>Listing-1</h5>
</p>
<p>When I compile this using Microsoft&#8217;s cl compiler as it ships with Visual Studio 2010 (Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64) here&#8217;s what I get:</p>
<pre class="brush: plain; title: ; notranslate">
test.cpp(13) : error C2541: 'delete' : cannot delete
 objects that are not pointers
</pre>
<p>And here’s the screen shot:</p>
<p><a href="http://umairsd.files.wordpress.com/2011/01/screen-shot-11.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0 0 2px;" title="Screen shot-1" src="http://umairsd.files.wordpress.com/2011/01/screen-shot-1_thumb.png?w=501&#038;h=269" border="0" alt="Screen shot-1" width="501" height="269" /></a></p>
<p>What is going on? Why doesn&#8217;t the compiler complain at both lines 10 and 13? It turns out, if a user-defined type has an explicit destructor, then the Visual Studio compiler happily compiles the double delete expression. It appears that in such a case the compiler violates the C++ specifications as the &#8220;delete ptr-to-type&#8221; returns a non-void pointer instead of void.</p>
<p>Suppose I get my code to compile by getting rid of the offending Bar object (code shown below). When I run my program, it is guaranteed to crash as we are calling delete on a ptr to unallocated space!</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
class Foo {
	public: ~Foo() { std::cout&lt;&lt;&quot;In dtor&quot;&lt;&lt;std::endl; }
};

int main(){
	Foo *pf = new Foo();
    delete delete pf;		//Uh Oh!

    std::cout&lt;&lt;&quot;Hello world&quot;&lt;&lt;std::endl;
    return 0;
}
</pre>
<h5>Listing-2</h5>
</p>
<p>And here&#8217;s the screen shot of the crash. Notice that as expected, the &#8220;<span style="font-family:Courier New;">Hello world</span>&#8221; line never gets executed.</p>
<p><a href="http://umairsd.files.wordpress.com/2011/01/screen-shot-21.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0 0 2px;" title="Screen shot-2" src="http://umairsd.files.wordpress.com/2011/01/screen-shot-2_thumb.png?w=508&#038;h=308" border="0" alt="Screen shot-2" width="508" height="308" /></a></p>
<p>I tried out the same code in gcc (i686-apple-darwin10-gcc-4.2.1), and gcc behaves correctly, i.e. it errors out on both lines 10 and 13 in the listing-1 above.</p>
<p>I am not sure if this behavior of the Visual Studio compiler is intentional. What do you think? Have you encountered any weird C++ compiler behaviors? Please share your thoughts in the comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=172&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2011/01/31/incorrect-c-compiler-behavior-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/01/screen-shot-1_thumb.png" medium="image">
			<media:title type="html">Screen shot-1</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/01/screen-shot-2_thumb.png" medium="image">
			<media:title type="html">Screen shot-2</media:title>
		</media:content>
	</item>
		<item>
		<title>Stern-Brocot Tree</title>
		<link>http://umairsaeed.com/2011/01/27/stern-brocot-tree/</link>
		<comments>http://umairsaeed.com/2011/01/27/stern-brocot-tree/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 13:56:15 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Algos]]></category>
		<category><![CDATA[Arithmetic]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=151</guid>
		<description><![CDATA[Stern-Brocot tree is a tree data structure whose vertices correspond to the set of non-negative rational numbers. Thus, this tree provides a very elegant way for constructing the set of fractions m/n, where m and n are relatively prime. To &#8230; <a href="http://umairsaeed.com/2011/01/27/stern-brocot-tree/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=151&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Stern-Brocot tree is a tree data structure whose vertices correspond to the set of non-negative rational numbers. Thus, this tree provides a very elegant way for constructing the set of fractions m/n, where m and n are relatively prime. To construct the tree, the basic idea is to start with two fractions (0/1, 1/0) and then repeat the following operation:</p>
<p><em>Insert (m+m&#8217;)/(n+n&#8217;) between two adjacent fractions m/n and m&#8217;/n&#8217;</em></p>
<p>The first step gives us the entry 1/1 between 0/1 and 1/0. Similarly, the 2nd step gives us two more: 0/1,1/2,1/1,2/1,1/0</p>
<p>Continuing on like this results in an infinite binary search tree which preserves the usual ordering of rational numbers.</p>
<p>The figure below shows the 1st 4 levels of the Stern-Brocot tree.</p>
<p><img title="Stern-Brocot.png" src="http://umairsd.files.wordpress.com/2011/01/stern-brocot.png?w=500&#038;h=340" alt="Stern-Brocot.png" width="500" height="340" border="0" /></p>
<h3>Finding the Path to k in Stern-Brocot Tree</h3>
<p>The path from the root of the tree to a number k in the Stern-Brocot tree can be found using binary search. At each node, k will either be in the left half of the tree, or the right half. We continue down the left or right subtree until we finally find k.</p>
<ul>
<li>Initialize the left fraction L to 0/1 and right fraction R to 1/0</li>
<li>Repeat the following until k is found:
<ul>
<li>Compute the mediant M (which is (m+m&#8217;)/(n+n&#8217;) )</li>
<li>If (k&gt;M), then k is in the right half of the tree. L:=M and continue.</li>
<li>Else If (M&gt;k), then k is in the left half of the tree. R:=M and continue.</li>
<li>Else k=M, terminate search.</li>
</ul>
</li>
</ul>
<p><strong>Implementation</strong></p>
<p>There&#8217;s a couple of things to tackle in our implementation. First, I need an easy way to represent fractions, so I create my own <em>SternBrocotFraction</em> class. I deliberately chose to make it very specific to this algorithm because I needed a special way to handle the fraction 1/0 (which by definition is greater than all other rationals).</p>
<p>Secondly, I needed a good way to represent the path from the root of the tree to k. I do this by using a <em>StringBuilder</em>, and at each step I append either the letter &#8216;L&#8217; or &#8216;R&#8217; depending on which sub-tree we take. When the search is finished, this gives us a <em>String</em> representation of the path from the root of the tree to the number k. This approach is similar to the approach advocated by ACM Programming Competitions for <a href="http://acm.uva.es/p/v100/10077.html">the &#8220;Stern-Brocot Number System&#8221; problem</a>.</p>
<p>Here&#8217;s the code to find path to a number k:</p>
<pre class="brush: java; title: ; notranslate">
package com.umairsaeed.algorithm;

public class SternBrocotPath {
	private static final char LEFT_SUB = 'L';
	private static final char RIGHT_SUB = 'R';

	public String findPathTo(SternBrocotFraction f) {
		SternBrocotFraction L = new SternBrocotFraction(0, 1);
		SternBrocotFraction R = new SternBrocotFraction(1, 0);

		StringBuilder results = new StringBuilder();
		SternBrocotPath.find(f, L, R, results);
		return results.toString();
	}

	public static void find(SternBrocotFraction f,
			SternBrocotFraction L,
			SternBrocotFraction R,
			StringBuilder results)
	{
		SternBrocotFraction M = L.add(R);

		if (M.compareTo(f) &lt; 0) {
			L = M;
			results.append(RIGHT_SUB);
			SternBrocotPath.find(f, L, R, results);
		} else if (M.compareTo(f) &gt; 0) {
			R = M;
			results.append(LEFT_SUB);
			SternBrocotPath.find(f, L, R, results);
		}
		return;
	}
}
</pre>
<p>The special SternBrocotFraction class is:</p>
<pre class="brush: java; title: ; notranslate">
package com.umairsaeed.algorithm;

public class SternBrocotFraction implements
				Comparable&lt;SternBrocotFraction&gt; {
	private int numerator;
	private int denominator;

	public SternBrocotFraction(int numerator, int denominator) {
		if (denominator &lt; 0) {
			numerator *= -1;
			denominator *= -1;
		}

		this.numerator = numerator;
		this.denominator = denominator;
	}

	public double doubleValue() {
		if (this.denominator == 0) {
			return Double.MAX_VALUE;
		} else {
			return (double) this.numerator /
						(double) this.denominator;
		}
	}

	public SternBrocotFraction add(SternBrocotFraction other) {
		return new SternBrocotFraction(
				this.numerator + other.numerator,
				this.denominator + other.denominator);
	}

	public int compareTo(SternBrocotFraction other) {
		if (this.doubleValue() &lt; other.doubleValue()) {
			return -1;
		} else if (this.doubleValue() &gt; other.doubleValue()) {
			return 1;
		}
		return 0;
	}
}
</pre>
<p>Finally, some test code to exercise my class:</p>
<pre class="brush: java; title: ; notranslate">
package com.umairsaeed.algorithm;

public class SternBrocotTester {
	public static void main(String[] args) {
		testSternBrocotPath();
	}

	public static void testSternBrocotPath() {
		SternBrocotPath t = new SternBrocotPath();

		SternBrocotFraction f = new SternBrocotFraction(5, 7);
		System.out.println(t.findPathTo(f));

		f = new SternBrocotFraction(19, 101);
		System.out.println(t.findPathTo(f));

		f = new SternBrocotFraction(977, 331);
		System.out.println(t.findPathTo(f));

		f = new SternBrocotFraction(1049, 7901);
		System.out.println(t.findPathTo(f));
	}
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/151/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=151&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2011/01/27/stern-brocot-tree/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/01/stern-brocot.png" medium="image">
			<media:title type="html">Stern-Brocot.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Manual Memory Management in Objective-C</title>
		<link>http://umairsaeed.com/2011/01/24/manual-memory-management-in-objective-c/</link>
		<comments>http://umairsaeed.com/2011/01/24/manual-memory-management-in-objective-c/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 19:10:40 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=147</guid>
		<description><![CDATA[Objective-C on iOS has no garbage collector, so it is up to the programmer to make sure that memory is properly freed once an object is no longer needed. On the other hand, Objective-C on the Mac does have a &#8230; <a href="http://umairsaeed.com/2011/01/24/manual-memory-management-in-objective-c/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=147&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Objective-C on iOS has no garbage collector, so it is up to the programmer to make sure that memory is properly freed once an object is no longer needed. On the other hand, Objective-C on the Mac does have a garbage collector (in Objective C 2.0). This blog post focuses on how to manage memory in the absence of a garbage collector.</p>
<p>When managing memory manually, two major issues to watch out for are premature deallocation and memory leaks.</p>
<p>Cocoa Touch framework uses manual <a href="http://en.wikipedia.org/wiki/Reference_counting">reference counting</a> to manage memory on the iOS devices. Reference counting works on the principle that once created, every object has an owner. During its existence, its owner may change and it may even have more than one owners. When the number of owners for an object drops to zero, it deallocates itself to free up the memory being used.</p>
<p><strong>retain &amp; release:</strong></p>
<p>Owners are tracked via retain counts. When an object is created it always has a retain count of 1. To own an object its retain count is incremented via the <em>retain</em> message. On the other hand, when the object is no longer needed its ownership is relinquished by decrementing the retain count via the <em>release</em> message. When the count reaches zero, the object sends itself the <em>dealloc</em> message and returns all the memory back to the heap.</p>
<p><strong>autorelease:</strong></p>
<p><em>autorelease</em> marks an object for future release (delayed release). When an object is sent the autorelease message, its retain count is incremented and it is added to an instance of <em>NSAutoreleasePool</em>. The Autorelease pool keeps track of all the objects that have been sent the <em>autorelease</em> message. This pool is drained periodically, at which time all the objects within it are sent the release message.</p>
<p>autorelease is really handy when the creator of an object (e.g. a factory) simply creates the object and returns it to the caller. At this point, the creator has nothing to do with the object anymore, so it is up to the caller to retain the returned object in order to continue using it.</p>
<h3>An Example &#8211; A Ticket class</h3>
<p>Let us work through an example to see manual memory management in action. Suppose I am writing a ticketing framework, and I have a Ticket entity. The header file for Ticket looks as:</p>
<pre class="brush: objc; title: ; notranslate">
@interface Ticket : NSObject {
	NSString *ticketId;
}

- (NSString *) ticketId;
- (void) setTicketId:(NSString *) tid;

- (id) initWithId:(NSString *) tid;
+ (id) ticketWithId:(NSString *) tid;

@end
</pre>
<p>And the implementation file for Ticket looks as follows:</p>
<pre class="brush: objc; title: ; notranslate">
#import &quot;Ticket.h&quot;
@implementation Ticket

- (void) dealloc
{
	[ticketId release];
	[super dealloc];
}

- (NSString *) ticketId
{
	return ticketId;
}

- (void) setTicketId:(NSString *)tid
{
	[tid retain];
	[ticketId release];
	ticketId = tid;
}

- (id) initWithId:(NSString *) tid
{
	if (self = [super init])
	{
		[self setTicketId:tid];
	}

	return self;
}

+ (id) ticketWithId:(NSString *)tid
{
	Ticket *newTkt = [[Ticket alloc] initWithId:tid];
	return [newTkt autorelease];
}

@end
</pre>
<p>There are three memory management points to note here:</p>
<p>1. Inside <em>dealloc</em>, an object must release all its instance variables first. Then it should go up its class hierarchy and release any instance variables of its superclass. We should never send the <em>dealloc</em> message to instance variables, as some other objects might still have references to those variables.</p>
<p>2. A setter must retain the value passed in before it releases the old value, as <em>tid</em> and <em>ticketId</em> could be pointers to the same object.</p>
<p>3.  The <em>ticketWithId:</em> method creates a ticket and simply returns it to the caller. It has no use for the <em>newTkt</em>, but it owns <em>newTkt</em> by virtue of creating it. At this point, if <em>newTkt</em> were released before method-exit, then the caller would get a pointer to unallocated heap. To avoid this, we put the <em>newTkt</em> on the autorelease pool, thus incrementing the retain count to 1. Periodically, the autorelease pool is drained and all the objects it it are sent the message release thus decrementing the retain count.</p>
<p>Essentially, the <em>ticketWithId:</em> method is saying that it does not want to be the owner for <em>newTkt</em> and puts that responsibility on the caller. If the caller wants to hold on to <em>newTkt</em> once it is returned, it must send it the <em>retain</em> message.</p>
<h3>Using the Ticket class:</h3>
<pre class="brush: objc; title: ; notranslate">
// EXAMPLE-1
- (void) processTicketWithId:(NSString *)ticketId
{
	Ticket* tkt = [[Ticket alloc] initWithId:ticketId];
	// Do something with tkt
	// ...
	[tkt release];
}
</pre>
<p>At this point, the retain count for <em>tkt</em> is 1. Moreover, since the <em>processTicketWithId:</em> method created the <em>tkt</em> object, it is now the owner and thus is responsible for cleaning it up before this method exits. Clean up is done by sending it the <em>release</em> message</p>
<p>Let&#8217;s see another example:</p>
<pre class="brush: objc; title: ; notranslate">
// EXAMPLE-2
- (void) processTicketWithId:(NSString *)ticketId
{
	Ticket* tkt = [Ticket ticketWithId:ticketId];
	[tkt retain];
	// Do something with tkt
	// ...
	[tkt release];
}
</pre>
<p>In this example, the memory for <em>tkt</em> wasn&#8217;t allocated by processTicketWithId: method, so it doesn&#8217;t own the <em>tkt</em> object. However, as we&#8217;ve seen in the implementation of the Ticket class, the <em>ticketWithId:</em> method created the Ticket object and added it to the autorelease pool. In order to continue using <em>tkt</em>, we must retain it so that even if it is drained from the autorelease pool, we can still continue to use the <em>tkt</em> object. Once done, we need to clean up and send the release message.</p>
<h3>Summary of Memory Management Rules for Objective-C:</h3>
<p>Rule-1: If you get/create the object from <em>new</em>, <em>alloc</em> or <em>copy</em>, you must <em>release</em> it when done.</p>
<p>Rule-2: If you get the object any other way, assume that it has been <em>autoreleased</em>. If you want to hold on to this object, send it the <em>retain</em> message.</p>
<p>Rule-3: If you <em>retain</em> an object, you must balance every retain with a <em>release</em>.</p>
<p>Rule-4: Never send the <em>dealloc</em> message to an object directly. Others might be holding references to this object, and if deallocated, they&#8217;ll be left with pointers to unallocated memory.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=147&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2011/01/24/manual-memory-management-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>
	</item>
		<item>
		<title>Snapshots in Xcode</title>
		<link>http://umairsaeed.com/2011/01/12/snapshots-in-xcode/</link>
		<comments>http://umairsaeed.com/2011/01/12/snapshots-in-xcode/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 18:47:35 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[Snapshots]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=141</guid>
		<description><![CDATA[When writing code, I usually write code in an incremental fashion. I try to get one set of functionality to work before I start on the next set. Due to this, I frequently create incremental backups of my files/project before &#8230; <a href="http://umairsaeed.com/2011/01/12/snapshots-in-xcode/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=141&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When writing code, I usually write code in an incremental fashion. I try to get one set of functionality to work before I start on the next set. Due to this, I frequently create incremental backups of my files/project before I begin the next set of major changes or mass edits. This allows me to come back to a clean and consistent state if I ever need to.</p>
<p>I typically don&#8217;t work on a private branch, so I cannot check in my code until I am reasonably done. To create temporary incremental backups, I usually just end up making copies of my project folder.</p>
<p>Xcode has a handy feature called Snapshot that let&#8217;s you create a safety net before you make any major or risky changes to your files. Go to File-&gt;Make Snapshot (shortcut: command-control-s), and Xcode remembers the state of the project.</p>
<p><img title="Screen shot-1.png" src="http://umairsd.files.wordpress.com/2011/01/screen-shot-1.png?w=702&#038;h=632" border="0" alt="Screen shot-1.png" width="702" height="632" /></p>
<p>Now we are free to make changes to the source files all we want. If we need to return to the previous state we use the snapshot window. This window can be invoked from File-&gt;Snapshots</p>
<p><img title="Screen shot-2.png" src="http://umairsd.files.wordpress.com/2011/01/screen-shot-2.png?w=302&#038;h=609" border="0" alt="Screen shot-2.png" width="302" height="609" /></p>
<p>Simply select the snapshot that you want, and press the Restore button. Xcode automatically creates another snapshot when you restore to an older snapshot, which allows you to jump back and forth between two different versions of the project. Pretty useful!</p>
<p>Snapshots are stored in a disk image that lives in ~/Library/Application Support/Developer/Shared/SnapshotRepository.sparseimage</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/141/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=141&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2011/01/12/snapshots-in-xcode/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/01/screen-shot-1.png" medium="image">
			<media:title type="html">Screen shot-1.png</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2011/01/screen-shot-2.png" medium="image">
			<media:title type="html">Screen shot-2.png</media:title>
		</media:content>
	</item>
		<item>
		<title>2010 in review</title>
		<link>http://umairsaeed.com/2011/01/11/2010-in-review/</link>
		<comments>http://umairsaeed.com/2011/01/11/2010-in-review/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 11:38:56 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://umairsaeed.com/?p=136</guid>
		<description><![CDATA[The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here&#8217;s a high level summary of its overall blog health: The Blog-Health-o-Meter™ reads This blog is doing awesome!. Crunchy numbers A Boeing 747-400 passenger jet &#8230; <a href="http://umairsaeed.com/2011/01/11/2010-in-review/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=136&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here&#8217;s a high level summary of its overall blog health:</p>
<p><img style="border:1px solid #ddd;background:#f5f5f5;padding:20px;" src="http://s0.wp.com/i/annual-recap/meter-healthy2.gif" alt="Healthy blog!" width="250" height="183" /></p>
<p>The <em>Blog-Health-o-Meter™</em> reads This blog is doing awesome!.</p>
<h2>Crunchy numbers</h2>
<p><a href="http://umairsd.files.wordpress.com/2010/01/screen_before_thumb21.png"><img style="max-height:230px;float:right;border:1px solid #ddd;background:#fff;margin:0 0 1em 1em;padding:6px;" src="http://umairsd.files.wordpress.com/2010/01/screen_before_thumb21.png?w=288" alt="Featured image" /></a></p>
<p>A Boeing 747-400 passenger jet can hold 416 passengers.  This blog was viewed about <strong>4,300</strong> times in 2010.  That&#8217;s about 10 full 747s.</p>
<p>&nbsp;</p>
<p>In 2010, there were <strong>12</strong> new posts, growing the total archive of this blog to 16 posts. There were <strong>21</strong> pictures uploaded, taking up a total of 2mb. That&#8217;s about 2 pictures per month.</p>
<p>The busiest day of the year was August 5th with <strong>69</strong> views. The most popular post that day was <a style="color:#08c;" href="http://umairsaeed.com/2010/02/03/creating-a-custom-xaml-typeconverter/">Creating a Custom XAML TypeConverter</a>.</p>
<p>&nbsp;</p>
<h2>Where did they come from?</h2>
<p>The top referring sites in 2010 were <strong>dotnet.dzone.com</strong>, <strong>dzone.com</strong>, <strong>google.co.in</strong>, <strong>google.com</strong>, and <strong>twitter.com</strong>.</p>
<p>Some visitors came searching, mostly for <strong>xaml typeconverter</strong>, <strong>typeconverter xaml</strong>, <strong>visual studio stl port debug</strong>, <strong>xaml type converter</strong>, and <strong>silverlight typeconverter</strong>.</p>
<div style="clear:both;"></div>
<h2>Attractions in 2010</h2>
<p>These are the posts and pages that got the most views in 2010.</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">1</div>
<p><a style="margin-right:10px;" href="http://umairsaeed.com/2010/02/03/creating-a-custom-xaml-typeconverter/">Creating a Custom XAML TypeConverter</a> <span style="color:#999;font-size:8pt;">February 2010</span></p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">2</div>
<p><a style="margin-right:10px;" href="http://umairsaeed.com/2009/11/23/visualizing-stlport-data-structures-in-visual-studio-debugger/">Visualizing STLPort data structures in Visual Studio Debugger</a> <span style="color:#999;font-size:8pt;">November 2009</span><br />
2 comments</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">3</div>
<p><a style="margin-right:10px;" href="http://umairsaeed.com/2010/01/20/xaml-and-typeconverters-2/">XAML and TypeConverters</a> <span style="color:#999;font-size:8pt;">January 2010</span><br />
2 comments</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">4</div>
<p><a style="margin-right:10px;" href="http://umairsaeed.com/2010/04/16/dependency-properties/">Dependency Properties in Silverlight</a> <span style="color:#999;font-size:8pt;">April 2010</span><br />
3 comments</p>
<div style="clear:left;float:left;font-size:24pt;line-height:1em;margin:-5px 10px 20px 0;">5</div>
<p><a style="margin-right:10px;" href="http://umairsaeed.com/2010/06/22/code-snippets-in-visual-studio-2010/">Code Snippets in Visual Studio 2010</a> <span style="color:#999;font-size:8pt;">June 2010</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/136/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=136&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2011/01/11/2010-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://s0.wp.com/i/annual-recap/meter-healthy2.gif" medium="image">
			<media:title type="html">Healthy blog!</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2010/01/screen_before_thumb21.png?w=288" medium="image">
			<media:title type="html">Featured image</media:title>
		</media:content>
	</item>
		<item>
		<title>Xcode &#8211; How to bring back the splash screen at launch</title>
		<link>http://umairsaeed.com/2010/09/14/xcode-how-to-bring-back-the-splash-screen-at-launch/</link>
		<comments>http://umairsaeed.com/2010/09/14/xcode-how-to-bring-back-the-splash-screen-at-launch/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 19:27:39 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/?p=132</guid>
		<description><![CDATA[When Xcode is started, I like the fact that a splash screen comes up with a list of recent projects (shown below). I somehow unchecked the &#8220;show at launch&#8221; box, and now Xcode starts up in headless mode. I checked &#8230; <a href="http://umairsaeed.com/2010/09/14/xcode-how-to-bring-back-the-splash-screen-at-launch/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=132&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When Xcode is started, I like the fact that a splash screen comes up with a list of recent projects (shown below). I somehow unchecked the &#8220;show at launch&#8221; box, and now Xcode starts up in headless mode.</p>
<p>
<img src="http://umairsd.files.wordpress.com/2010/09/xcode-splash1.png?w=500&#038;h=326" alt="Xcode Splash.png" title="Xcode Splash.png" border="0" width="500" height="326" /></p>
<p>
I checked all the preferences for Xcode, but could not figure out a way to bring the Splash screen back. It turns out, its not very intuitive. In order to bring the splash screen back, I needed to edit the <a href="http://en.wikipedia.org/wiki/Property_list">property list file</a> for Xcode. Navigate to ~/Library/Preferences and double click the com.apple.Xcode.plist property file. The files opens up in Property List Editor as show below:</p>
<p>
<img src="http://umairsd.files.wordpress.com/2010/09/propertylisteditor.png?w=500&#038;h=520" alt="PropertyListEditor.png" title="PropertyListEditor.png" border="0" width="500" height="520" /></p>
<p>
Scroll down to the property XCShowSplashScreen, and check its value box. Save the plist file, relaunch Xcode, and the splash screen should now show up.</p>
<p>Just a handy tip that I thought I should share.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=132&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2010/09/14/xcode-how-to-bring-back-the-splash-screen-at-launch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2010/09/xcode-splash1.png" medium="image">
			<media:title type="html">Xcode Splash.png</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2010/09/propertylisteditor.png" medium="image">
			<media:title type="html">PropertyListEditor.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Code Snippets in Visual Studio 2010</title>
		<link>http://umairsaeed.com/2010/06/22/code-snippets-in-visual-studio-2010/</link>
		<comments>http://umairsaeed.com/2010/06/22/code-snippets-in-visual-studio-2010/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 12:58:46 +0000</pubDate>
		<dc:creator>Umair</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">https://umairsd.wordpress.com/2010/06/22/code-snippets-in-visual-studio-2010/</guid>
		<description><![CDATA[Visual Studio allows developers to save snippets of code which can be inserted at a later time. This saves on retyping blocks of code that are used often. I also find it very useful when I have to show code &#8230; <a href="http://umairsaeed.com/2010/06/22/code-snippets-in-visual-studio-2010/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=118&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Visual Studio allows developers to save snippets of code which can be inserted at a later time. This saves on retyping blocks of code that are used often. I also find it very useful when I have to show code during presentations. Instead of typing everything live, I find it far easier to simply add the code block by block using code snippets. </p>
<p>VS 2010 improves the snippet functionality, and makes it easier to create code snippets. There&#8217;s two types of snippets: </p>
<p>- Expansion snippets are inserted at the cursor   <br />- SurroundsWith snippets wraps around selected code</p>
<p>&#160;</p>
<p><strong>Creating a Custom Snippet:     <br /></strong>Let&#8217;s go ahead and create a try-catch-finally Expansion snippet: </p>
<p>- Insert a new XML file to the project and call it TryCatchFinally.snippet. Make sure the file name ends with .snippet </p>
<p>- Right-click on the editor window and select Insert Snippet-&gt;Snippet. This creates a basic XML snippet template as shown below:</p>
<pre>
<pre class="brush: xml; auto-links: false; title: ; notranslate">
&lt;CodeSnippet Format=&quot;1.0.0&quot; 
             xmlns=&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;&gt;
  &lt;Header&gt;
    &lt;Title&gt;title&lt;/Title&gt;
    &lt;Author&gt;author&lt;/Author&gt;
    &lt;Shortcut&gt;shortcut&lt;/Shortcut&gt;
    &lt;Description&gt;description&lt;/Description&gt;
    &lt;SnippetTypes&gt;
      &lt;SnippetType&gt;SurroundsWith&lt;/SnippetType&gt;
      &lt;SnippetType&gt;Expansion&lt;/SnippetType&gt;
    &lt;/SnippetTypes&gt;
  &lt;/Header&gt;
  &lt;Snippet&gt;
    &lt;Declarations&gt;
      &lt;Literal&gt;
        &lt;ID&gt;name&lt;/ID&gt;
        &lt;Default&gt;value&lt;/Default&gt;
      &lt;/Literal&gt;
    &lt;/Declarations&gt;
    &lt;Code Language=&quot;XML&quot;&gt;
      &lt;![CDATA[&lt;test&gt;
      &lt;name&gt;$name$&lt;/name&gt;
      $selected$ $end$&lt;/test&gt;]]&gt;
    &lt;/Code&gt;
  &lt;/Snippet&gt;
&lt;/CodeSnippet&gt;
</pre>
<p>- As you can see, by default the template contains both snippet types. Since I am creating an Expansion snippet, let's go ahead and get rid of the &lt;SnippetType&gt;SurroundsWith&lt;/SnippetType&gt; tag. </p>
<p>- Update the Title to &quot;Try Catch Finally&quot;, Shortcut to &quot;trycf&quot; and Description to &quot;Adds a try-catch-finally block&quot;. </p>
<p>- Literal tag enables us to define editable values that are inserted into the snippet. In the try-catch-finally snippet, we want to allow the user to change the type of the Exception being caught. The ID tag is the name of the editable value so change it to &quot;ExceptionName&quot;. The Default tag specifies the default value for our editable field. I want to catch all exceptions, so change the Default tag's value to &quot;Exception&quot;. </p>
<p>- Finally, the code section contains what will be added when the snippet is inserted. Change the language to &quot;CSharp&quot;, and the body to the following:</p>
<pre>
<pre class="brush: xml; auto-links: false; title: ; notranslate">
    &lt;Code Language=&quot;CSharp&quot;&gt;
      &lt;![CDATA[
try
{

}
catch($ExceptionName$)
{

}
finally
{

}
      ]]&gt;
    &lt;/Code&gt;
</pre>
<p>- The final version of the TryCatchFinally.snippet file is shown below. For more details about the XML schema, take a look at the <a href="http://msdn.microsoft.com/en-us/library/ms171418%28v=VS.80%29.aspx">MSDN code snippets Schema Reference</a> </p>
<pre>
<pre class="brush: xml; auto-links: false; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;CodeSnippet Format=&quot;1.0.0&quot; 
             xmlns=&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;&gt;
  &lt;Header&gt;
    &lt;Title&gt;Try Catch Finally&lt;/Title&gt;
    &lt;Author&gt;umair&lt;/Author&gt;
    &lt;Shortcut&gt;trycf&lt;/Shortcut&gt;
    &lt;Description&gt;Adds a try-catch-finally block&lt;/Description&gt;
    &lt;SnippetTypes&gt;
      &lt;SnippetType&gt;Expansion&lt;/SnippetType&gt;
    &lt;/SnippetTypes&gt;
  &lt;/Header&gt;
  &lt;Snippet&gt;
    &lt;Declarations&gt;
      &lt;Literal&gt;
        &lt;ID&gt;ExceptionName&lt;/ID&gt;
        &lt;Default&gt;Exception&lt;/Default&gt;
      &lt;/Literal&gt;
    &lt;/Declarations&gt;
    &lt;Code Language=&quot;CSharp&quot;&gt;
      &lt;![CDATA[
try
{

}
catch($ExceptionName$)
{

}
finally
{

}
      ]]&gt;
    &lt;/Code&gt;
  &lt;/Snippet&gt;
&lt;/CodeSnippet&gt;
</pre>
<p><strong>Loading the Snippet into Visual Studio:</strong></p>
<p>There are two ways to insert the above snippet into Visual Studio. </p>
<p>The most straightforward way is to put the .snippet files in VS 2010's code snippets directory. The default location for it is <font face="Courier New">C:\Users\&lt;UserName&gt;\Documents\Visual Studio 2010\Code Snippets\</font>. This folder contains sub-directories based on the language of the snippet. For my example, the snippet goes under Visual C#. Once put here, VS 2010 automatically picks up the snippets without the need to restart. </p>
<p>&#160;</p>
<p>The second option is to import the .snippet file into Visual Studio. </p>
<p>- Inside Visual studio, go to Tools-&gt;Code Snippets Manager (Ctrl+K,Ctrl+B). This brings up the Snippets Manager.<br />
  <br />- Press Import button, and navigate to the location where the snippet was saved. Select the .snippet file.</p>
<p>- Press OK.</p>
<p>&#160;</p>
<p><strong>Using the Code Snippet:</strong></p>
<p>Type the name of the snippet (the shortcut), and pressing tab expands it. </p>
<p><a href="http://umairsd.files.wordpress.com/2010/06/snippet1.png"><img src="http://umairsd.files.wordpress.com/2010/06/snippet1.png?w=500&#038;h=516" alt="Intellisense after typing snippet name" title="Snippet1" width="500" height="516" class="alignnone size-full wp-image-122" /></a></p>
<p>You can also press Ctrl+K and Ctrl+X which brings up the &quot;Insert Snippet&quot; menu as shown below: </p>
<p><a href="http://umairsd.files.wordpress.com/2010/06/snippet2.png"><img src="http://umairsd.files.wordpress.com/2010/06/snippet2.png?w=500" alt="" title="Snippet2"   class="alignnone size-full wp-image-124" /></a></p>
<p>We can navigate to My Code Snippets, and select the TryCatchFinally that we've just added:</p>
<p><a href="http://umairsd.files.wordpress.com/2010/06/snippet3.png"><img src="http://umairsd.files.wordpress.com/2010/06/snippet3.png?w=500&#038;h=190" alt="" title="Snippet3" width="500" height="190" class="alignnone size-full wp-image-125" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umairsd.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umairsd.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umairsaeed.com&#038;blog=9333327&#038;post=118&#038;subd=umairsd&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umairsaeed.com/2010/06/22/code-snippets-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3d8419a4ace3305d6791158454f7f874?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umairsd</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2010/06/snippet1.png" medium="image">
			<media:title type="html">Snippet1</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2010/06/snippet2.png" medium="image">
			<media:title type="html">Snippet2</media:title>
		</media:content>

		<media:content url="http://umairsd.files.wordpress.com/2010/06/snippet3.png" medium="image">
			<media:title type="html">Snippet3</media:title>
		</media:content>
	</item>
	</channel>
</rss>
