{"id":25553,"date":"2007-08-16T10:00:00","date_gmt":"2007-08-16T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/08\/16\/what-are-these-strange-cmp-ecx-ecx-instructions-doing-in-my-c-code\/"},"modified":"2007-08-16T10:00:00","modified_gmt":"2007-08-16T10:00:00","slug":"what-are-these-strange-cmp-ecx-ecx-instructions-doing-in-my-c-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070816-00\/?p=25553","title":{"rendered":"What are these strange cmp [ecx], ecx instructions doing in my C# code?"},"content":{"rendered":"<p>When you debug through some managed code at the assembly level, you&#8217;ll find a whole lot of seemingly pointless instructions that perform a comparison but ignore the result. What&#8217;s the point of comparing two values if you don&#8217;t care what the result is?\n In C++, invoking an instance method on a <code>NULL<\/code> pointer results in undefined behavior. In other words, if you do it, the compiler is allowed to do anything it wants. And what most compilers do is, um, nothing. They don&#8217;t take any special steps if the <code>this<\/code> pointer is <code>NULL<\/code>; they just generate code on the assumption that it isn&#8217;t. In practice, this often means that everything seems to run just fine until you access a member variables or call a virtual functions, and then you crash.\n The C# language, by comparison, is <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/csspec\/html\/vclrfcsharpspec_7_4_3.asp\"> quite explicit about what happens if you invoke an instance method on a <code>null<\/code> object reference<\/a>:<\/p>\n<blockquote class=\"q\"><p> The value of <code>E<\/code> is checked to be valid. If the value of <code>E<\/code> is null, a <code>System.NullReferenceException<\/code> is thrown and no further steps are executed.  <\/p><\/blockquote>\n<p> The null reference exception must be thrown before the method can be called. That&#8217;s what the strange <code>cmp [ecx], ecx<\/code> comparison is for.&sup1; The compiler doesn&#8217;t actually care what the result of the comparison is; it just wants to raise an exception if <code>ecx<\/code> is null. If <code>ecx<\/code> is null, the attempt to dereference it (in order to perform the comparison) will raise an access violation, which the runtime inspects and turns into a <code>NullReferenceException<\/code>.\n The test is usually against the <code>ecx<\/code> register since the CLR happens to use&sup2; the <code>fastcall<\/code> calling convention, which for instance methods passes the <code>this<\/code> pointer in the <code>ecx<\/code> register. The pointer the compiler wants to test is going to wind up in the <code>ecx<\/code> register sooner or later,&sup3; so it&#8217;s not surprising that the test, when it happens, is made against the <code>ecx<\/code> register.\n <b>Nitpicker&#8217;s Corner<\/b>\n &sup1;Although this statement is written as if it were a fact, it is actually my interpretation based on observation and thinking about how language features are implemented. It is <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/08\/10\/4315707.aspx#4327521\"> not an official position<\/a> of the CLR team nor Microsoft Corporation, and that interpretation may ultimately prove incorrect.\n &sup2;&#8221;Happens to use&#8221; means that this is an implementation detail, not a contractual guarantee.&sup1;<\/p>\n<p> &sup3;Unless the call is optimized. For example, the function might be inlined. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you debug through some managed code at the assembly level, you&#8217;ll find a whole lot of seemingly pointless instructions that perform a comparison but ignore the result. What&#8217;s the point of comparing two values if you don&#8217;t care what the result is? In C++, invoking an instance method on a NULL pointer results in [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-25553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When you debug through some managed code at the assembly level, you&#8217;ll find a whole lot of seemingly pointless instructions that perform a comparison but ignore the result. What&#8217;s the point of comparing two values if you don&#8217;t care what the result is? In C++, invoking an instance method on a NULL pointer results in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/25553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=25553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/25553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=25553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=25553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=25553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}