{"id":5193,"date":"2013-02-20T07:00:00","date_gmt":"2013-02-20T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/02\/20\/you-can-ask-the-compiler-to-answer-your-calling-convention-questions\/"},"modified":"2013-02-20T07:00:00","modified_gmt":"2013-02-20T07:00:00","slug":"you-can-ask-the-compiler-to-answer-your-calling-convention-questions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130220-00\/?p=5193","title":{"rendered":"You can ask the compiler to answer your calling convention questions"},"content":{"rendered":"<p><P>\nIf you want to figure out some quirks of a calling\nconvention,\nyou can always ask the compiler to do it for you,\non the not unreasonable assumption that the compiler\nunderstands calling conventions.\n<\/P>\n<P>\n&#8220;When a <CODE>__stdcall<\/CODE>\nfunction returns a large structure by value,\nthere is a hidden first parameter that specifies the\naddress the return value should be stored.\nBut if the function is a C++ instance method,\nthen there is also a hidden <CODE>this<\/CODE>\nparameter.\nWhich goes first, the return value parameter\nor the <CODE>this<\/CODE> pointer?&#8221;\n<\/P>\n<P>\nThis is another case of\n<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/07\/27\/10190102.aspx\">\nYou don&#8217;t need to ask me a question the compiler can answer more accurately<\/A>.\n<\/P>\n<PRE>\nstruct LargeStructure\n{\n char x[256];\n};<\/p>\n<p>class Something\n{\npublic:\n LargeStructure __stdcall TestMe();\n};<\/p>\n<p>void foo(Something *something)\n{\n LargeStructure x = something-&gt;TestMe();\n}\n<\/PRE>\n<P>\nYou could compile this into a program and\nthen look in the debugger,\nor just ask the compiler to generate an assembly\nlisting.\nI prefer the assembly listing, since it saves a few\nsteps,\nand the compiler provides helpful symbolic names.\n<\/P>\n<PRE>\n  00015 mov     eax, DWORD PTR _something$[ebp]<\/p>\n<p>; LargeStructure x = something-&gt;TestMe();<\/p>\n<p>  00018 lea     ecx, DWORD PTR _x$[ebp]\n  0001e push    ecx\n  0001f push    eax\n  00020 call    ?TestMe@Something@@\n                QAG?AULargeStructure@@XZ\n                ; Something::TestMe<\/p>\n<p><\/PRE>\n<P>\nWe see that the last thing pushed onto the stack\n(and therefore the top parameter on the stack\nat the point of the call)\nis the <CODE>something<\/CODE> parameter,\nwhich is the <CODE>this<\/CODE>\nfor the function.\n<\/P>\n<P>\nConclusion:\nThe <CODE>this<\/CODE> pointer goes ahead of the\noutput structure pointer.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to figure out some quirks of a calling convention, you can always ask the compiler to do it for you, on the not unreasonable assumption that the compiler understands calling conventions. &#8220;When a __stdcall function returns a large structure by value, there is a hidden first parameter that specifies the address the [&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-5193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you want to figure out some quirks of a calling convention, you can always ask the compiler to do it for you, on the not unreasonable assumption that the compiler understands calling conventions. &#8220;When a __stdcall function returns a large structure by value, there is a hidden first parameter that specifies the address the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5193","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=5193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5193\/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=5193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}