{"id":864,"date":"2022-08-30T15:08:27","date_gmt":"2022-08-30T15:08:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/problem-on-dword-ptr-with-visual-studio-2010-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:27","modified_gmt":"2022-08-30T15:08:27","slug":"problem-on-dword-ptr-with-visual-studio-2010-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/problem-on-dword-ptr-with-visual-studio-2010-collection-of-common-programming-errors\/","title":{"rendered":"Problem on dword ptr[&hellip;] with visual studio 2010-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to compile a program which worked with the compiler of Visual C++ 6.0. Now, I&#8217;m compiling it using the compiler of Visual Studio 2010. It compiles but my program crashes on assignment of a variable:<\/p>\n<pre><code>sapHigh = ctx-&gt;saphigh;\n<\/code><\/pre>\n<p>In assembler code (using Visual Studio Debugger), this instruction is decomposed by:<\/p>\n<pre><code>00410DF3  mov         eax,dword ptr [ctx]  \n00410DF6  mov         ecx,dword ptr [eax+20h]  \n00410DF9  mov         dword ptr [sapHigh],ecx \n<\/code><\/pre>\n<p>The instruction &#8220;mov eax, dword ptr[ctx]&#8221; return 0x00000000 in eax and the program crashes. But the debugger can see the real value of ctx pointer which is &#8220;0x0172287a&#8221;. If I change the value of the eax register with its real value, &#8220;0x0172287a&#8221;, the program still working fine until the next assignment.<\/p>\n<p>Does anybody know why this instruction doesn&#8217;t work ? Is this a problem with compilation options ?<\/p>\n<p>Thank you for your help.<\/p>\n<ol>\n<li>\n<p>That&#8217;s a long gap of 10-12 years. I recommend you compile the program in VC10 with ALL compiler warnings enabled, and attempt to remove all of them. It includes all warnings related to deprecated stuff, 64-bit warnings, performance warnings, and C++ compliant warnings.<\/p>\n<\/li>\n<li>\n<p>Sounds like the memory for the variable <code>ctx<\/code> is not longer valid. If <code>ctx<\/code> is for example part of some dynamically allocated struct that got freed earlier, or if it&#8217;s a reference or pointer to an out-of-scope stack variable, all kinds of undefined behaviors can happen.<\/p>\n<p>Using invalid memory might not crash if (by chance) no other functions overwrote that memory, but using a different compiler and a different runtime library can easily lead to different results.<\/p>\n<\/li>\n<li>\n<p>What if you try <code>lea eax, dword ptr[ctx]<\/code> ?<\/p>\n<p>Also I&#8217;ve seen problems like that happen if the pointer to the array is passed to a function. When the array is defined within a function everything&#8217;s fine.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:49:26. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to compile a program which worked with the compiler of Visual C++ 6.0. Now, I&#8217;m compiling it using the compiler of Visual Studio 2010. It compiles but my program crashes on assignment of a variable: sapHigh = ctx-&gt;saphigh; In assembler code (using Visual Studio Debugger), this instruction is decomposed by: 00410DF3 mov eax,dword [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}