{"id":3453,"date":"2011-02-08T20:02:00","date_gmt":"2011-02-08T20:02:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2011\/02\/08\/go-ask-printf-to-behave-predictably\/"},"modified":"2019-02-18T18:45:14","modified_gmt":"2019-02-18T18:45:14","slug":"go-ask-printf-to-behave-predictably","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/go-ask-printf-to-behave-predictably\/","title":{"rendered":"Go Ask printf() To Behave Predictably"},"content":{"rendered":"<p>This is one of those problems whose solution is very evident&hellip; or bitterly hard to guess depending on how much you were influenced by what you were told about it.<\/p>\n<p>I&rsquo;ll paste here the case<\/p>\n<table cellpadding=\"3\" cellspacing=\"1\" border=\"1\" style=\"width: 100%\">\n<tbody>\n<tr>\n<td width=\"100%\" valign=\"top\">\n<p><span style=\"font-family: Arial\">I must be getting blind, as I can&rsquo;t see what&rsquo;s wrong. This code works on amd64, but crashes on x86. <\/p>\n<p><\/span>\n<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9344fc6b-9394-436d-8fb6-daa5806b63cb\" style=\"padding-bottom: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;float: none;padding-top: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #ddd;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"color:#0000ff\">#include<\/span> <span style=\"color:#a31515\">&lt;regex&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">using<\/span> <span style=\"color:#0000ff\">namespace<\/span> std;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">int<\/span> main() {<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">char<\/span> searchMe[] = <span style=\"color:#a31515\">&#8220;Go ask printf() to behave predictably.&#8221;<\/span>;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;regex rx(<span style=\"color:#a31515\">&#8220;^Go ask &#8220;<\/span>);<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;cmatch found;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">if<\/span> (regex_search(searchMe, found, rx))<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color:#a31515\">&#8220;Found it: &#8220;%s&#8221;.n&#8221;<\/span>, found.str());<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">else<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color:#a31515\">&#8220;Didn&#8217;t find it.n&#8221;<\/span>);<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><span style=\"font-family: Arial\">I debugged a bit and found out that what is failing is actually the first printf, because formatting for %s is not apparently using the address of the string, but it tries to dereference its content.<\/span><\/p>\n<p><span style=\"font-family: Arial\">Can anyone help me see what&rsquo;s wrong?<\/span><\/p>\n<p><span style=\"font-family: Arial\">Thanks<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>You will rapidly solve it&hellip; or not! It will actually depend on what it impressed you the most about the symptoms.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is one of those problems whose solution is very evident&hellip; or bitterly hard to guess depending on how much you were influenced by what you were told about it. I&rsquo;ll paste here the case I must be getting blind, as I can&rsquo;t see what&rsquo;s wrong. This code works on amd64, but crashes on x86. [&hellip;]<\/p>\n","protected":false},"author":293,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[100],"class_list":["post-3453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-c-language"],"acf":[],"blog_post_summary":"<p>This is one of those problems whose solution is very evident&hellip; or bitterly hard to guess depending on how much you were influenced by what you were told about it. I&rsquo;ll paste here the case I must be getting blind, as I can&rsquo;t see what&rsquo;s wrong. This code works on amd64, but crashes on x86. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/293"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=3453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=3453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=3453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=3453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}