小编psp*_*int的帖子

如何在 C 中实现 Go 的 defer() 以便允许声明变量?

在这个现代 C视频中,有一个技巧可以推迟代码的执行,直到块/作用域退出。它的使用方法如下:

\n
int 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}\n
Run 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)\n
Run Code Online (Sandbox Code Playgroud)\n …

c gcc c-preprocessor c11 gcc-extensions

3
推荐指数
1
解决办法
2382
查看次数

为什么esp后面加的是0x10呢?

我正在阅读维基百科文章,并且无法理解为什么add esp, 0x10下面的代码块末尾有一个。我会放弃我自己的假设,只是问 \xe2\x80\x93 为什么?

\n
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)

x86 assembly stack callstack

1
推荐指数
1
解决办法
1111
查看次数

标签 统计

assembly ×1

c ×1

c-preprocessor ×1

c11 ×1

callstack ×1

gcc ×1

gcc-extensions ×1

stack ×1

x86 ×1