gpt4 book ai didi

c++ - 函数声明 __declspec(naked) 如何存储局部变量?

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:25 26 4
gpt4 key购买 nike

__declspec(naked) void printfive() {
int i = 5;
printf("%i\n", i);
}

出于某种原因这段代码有效,但我不明白 i 存储在哪里?在调用函数的框架中?变成全局变量?如果它存储在调用者的帧中,那么编译器如何知道位移,因为您可以从具有不同帧大小和局部变量的不同函数调用 printfive()。如果它是全局的,或者类似 static 的东西,我已经尝试递归并且我可以看到变量没有改变,它确实不是真正的局部变量。但是很明显,没有入口代码(prolog)。好的,我明白了,没有序言,没有框架,没有寄存器更改,但这是值,但是 scope 会发生什么?此说明符的行为是否在任何引用中定义?这是 C++ 标准的一部分吗?如果您主要在其中使用 asm {}(或使用 asm 调用它们并希望确保该函数没有过度优化),则此类函数非常有用,但您可以与 C++ 混合使用。但这是一种脑筋急转弯。

最佳答案

我知道这个话题已经有好几年了,这是我自己的答案。

由于没有引用有关此主题的 Microsoft 文档,对于那些想了解更多有关 Keltar 所述的需要或不需要的内容的人,here是解释 Keltar 此处未解释的大部分内容的 Microsoft 文档。

根据 Microsoft 文档,应避免使用。

The following rules and limitations apply to naked functions:

  • The return statement is not permitted.
  • Structured Exception Handling and C++ Exception Handling constructs are not permitted because they must unwind across the stack frame.
  • For the same reason, any form of setjmp is prohibited
  • Use of the _alloca function is prohibited.
  • To ensure that no initialization code for local variables appears before the prolog sequence, initialized local variables are not permitted at function scope. In particular, the declaration of C++
    objects are not permitted at function scope. There may, however, be initialized data in a nested scope.
  • Frame pointer optimization (the /Oy compiler option) is not recommended, but it is automatically suppressed for a naked function.
  • You cannot declare C++ class objects at the function lexical scope. You can, however, declare objects in a nested block.

关于c++ - 函数声明 __declspec(naked) 如何存储局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19725094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com