在这个现代 C视频中,有一个技巧可以推迟代码的执行,直到块/作用域退出。它的使用方法如下:
\nint main()\n{\n int foo=0, bar;\n const char *etc = "Some code before defer";\n\n defer(profile_begin(), profile_end())\n {\n /* Some code, which will be automatically \n * preceded by call to profile_begin() and\n * followed by run of profile_end().*/\n foo++;\n bar = 1;\n }\n\n etc = "Some code after defer";\n foo = bar + 1;\n}\nRun Code Online (Sandbox Code Playgroud)\n视频中的实现:
\n#define macro_var_line(name) concat(name, __LINE__)\n#define defer(start,end) for( \\\n int macro_var_line(done) = (start,0); \\\n !macro_var_line(done); \\\n (macro_var_line(done) += 1), end)\nRun Code Online (Sandbox Code Playgroud)\n … 我正在阅读维基百科文章,并且无法理解为什么add esp, 0x10下面的代码块末尾有一个。我会放弃我自己的假设,只是问 \xe2\x80\x93 为什么?
printnums:\n ; stack setup\n push ebp\n mov ebp, esp\n sub esp, 0x08\n mov [ebp-0x04], ecx ; in x86, ecx = first argument.\n mov [ebp-0x08], edx ; arg2\n push [ebp+0x08] ; arg3 is pushed to stack.\n push [ebp-0x08] ; arg2 is pushed\n push [ebp-0x04] ; arg1 is pushed\n push 0x8065d67 ; "The numbers you sent are %d %d %d"\n call printf\n ; stack cleanup\n add esp, 0x10\n nop\n leave\n retn 0x04\n …Run Code Online (Sandbox Code Playgroud)