gpt4 book ai didi

c++ - C++ 隐含了这个,以及它是如何被压入堆栈的

转载 作者:可可西里 更新时间:2023-11-01 18:03:46 32 4
gpt4 key购买 nike

我需要知道,当调用 C++ 中的类方法时,隐式“this”指针是第一个参数还是最后一个参数。即:它是先入栈还是最后入栈。

换句话说,我是在询问一个被调用的类方法是否被编译器认为是:

int foo::bar(foo *const this, int arg1, int arg2); 
//or:
int foo::bar(int arg1, int arg2, foo *const this);

因此,通过扩展,更重要的是,这也将回答 G++ 是将 this 指针分别压入最后还是最先。我查询了谷歌,但没有找到太多。

另外,当调用 C++ 函数时,它们是否与 C 函数执行相同的操作?即:

push ebp
mov ebp, esp

总而言之:被调用的类方法看起来像这样吗?

; About to call foo::bar.
push dword 0xDEADBEEF
push dword 0x2BADBABE
push dword 0x2454ABCD ; This one is the this ptr for the example.
; this code example would match up if the this ptr is the first argument.
call _ZN3foo3barEpjj

谢谢,非常感谢。

编辑:为了澄清问题,我使用的是 GCC/G++ 4.3

最佳答案

这取决于您的编译器的调用约定和目标架构。

默认情况下,Visual C++ 不会将其压入堆栈。对于 x86,编译器将默认使用“thiscall”调用约定并将其传递到 ecx 寄存器中。如果为成员函数指定 __stdcall,它将作为第一个参数压入堆栈。

对于 VC++ 上的 x64,前四个参数在寄存器中传递。这是第一个参数,传入rcx寄存器。

Raymond Chen 几年前有一个关于调用约定的系列。这是 x86x64文章。

关于c++ - C++ 隐含了这个,以及它是如何被压入堆栈的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1297205/

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