{"id":1240,"date":"2012-09-21T10:12:09","date_gmt":"2012-09-20T22:12:09","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1240"},"modified":"2012-09-21T10:13:26","modified_gmt":"2012-09-20T22:13:26","slug":"whats-in-a-word","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1240\/","title":{"rendered":"What&#8217;s in a Word &#8230; ?"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute]<\/span><\/span><p>In an exchange with David Heffernan both on SO and in the comments here on Te Waka, I had cause to climb in my own personal &#8220;Wayback Machine&#8221; and further investigate an apparent change in compiler behaviour between Delphi 2007 and 2009.<\/p>\n<p>This change was first identified as the result of <a href=\"http:\/\/stackoverflow.com\/questions\/1482699\/zeroconf-bonjour-code-that-works-in-delphi-7-not-working-in-2009\" target=\"_blank\">some code of mine that stopped working in Delphi 2009<\/a>.  The instinctive reaction is &#8220;It must be some Unicode issue&#8221;, but it turned out that the &#8220;problem&#8221; was actually a fix to a compiler bug!<\/p>\n<p><!--more--><\/p>\n<p>To illustrate the bug that was fixed, compile and run this code in Delphi 2007 or earlier and then compile and run it using Delphi 2009 or later:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n{$apptype CONSOLE}\r\n\r\nprogram overloads;\r\n\r\nfunction Foo(const aWord: Word): Word; overload;\r\nbegin\r\n  result := 42;\r\nend;\r\n\r\nfunction Foo(const aWord: Longword): Longword; overload;\r\nbegin\r\n  result := 0;\r\nend;\r\n\r\nvar\r\n  i: Integer;\r\nbegin\r\n  i := 300000;\r\n  WriteLn(Foo(i));\r\nend.\r\n<\/pre>\n<p>In Delphi 2007 and earlier, the compiler calls the <em>Word<\/em> version of <strong>Foo()<\/strong>, not the <em>LongWord<\/em>, even though <strong>i<\/strong> is declared as an <em>Integer<\/em> (32-bit) which clearly is too big for a <em>Word<\/em> (16-bit) sized parameter.<\/p>\n<p>It&#8217;s staggering to me that this problem had not bitten me in the rear appendage before that occasion in 2008.<\/p>\n<p>In hindsight it is even more staggering that the compiler did not &#8211; and does not &#8211; emit a warning when an ordinal value is truncated in this fashion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute]<\/span><\/span>In an exchange with David Heffernan both on SO and in the comments here on Te Waka, I had cause to climb in my own personal &#8220;Wayback Machine&#8221; and further investigate an apparent change in compiler behaviour between Delphi 2007 and 2009. This change was first identified as the result of some code of mine [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[187,292],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-compiler-bug","tag-delphi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-k0","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1240"}],"version-history":[{"count":3,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1240\/revisions"}],"predecessor-version":[{"id":1243,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1240\/revisions\/1243"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}