C ++的隐含这一点,它究竟是如何压入堆栈 [英] The C++ implicit this, and exactly how it is pushed on the stack

查看:241
本文介绍了C ++的隐含这一点,它究竟是如何压入堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道是否,何时在C ++类方法被调用时,隐式this指针是第一个参数,或最后。即:无论是压入堆栈第一个或最后一个

I need to know whether, when a class method in C++ is called, the implicit 'this' pointer is the first argument, or the last. i.e: whether it is pushed onto the stack first or last.

在换句话说,我要问一个类的方法,无论是被称为,采取的是编译器是:

In other words, I'm asking whether a class method, being called, is taken by the compiler to be:

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

通过因此延长,更重要的是,也将回答G ++是否将推动这个指针最后或第一,分别。我询问谷歌,但我没有找到多少。

By extension therefore, and more importantly, that would also answer whether G++ would push the this pointer last or first, respectively. I interrogated google, but I didn't find much.

和作为一个方面说明,当C ++函数被调用,他们做同样的事情,C函数?即:

And as a side note, when C++ functions are called, do they do the same thing as C functions? i.e:

push ebp
mov ebp, esp

所有的一切:?会被调用这个样子的一类方法

All in all: would a class method being called look like this?

; 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

谢谢,非常感谢。

Thanks, and much obliged.

编辑:澄清事情,我使用的GCC / G ++ 4.3

to clarify things, I'm using GCC/G++ 4.3

推荐答案

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

This depends on the calling convention of your compiler and the target architecture.

在默认情况下,Visual C ++会不会推动这个堆栈中。对于x86,编译器将默认为thiscall调用约定,将通过这个ECX寄存器。如果你对你的成员函数指定__stdcall,它会在堆栈作为第一个参数上推。

By default, Visual C++ will not push this on the stack. For x86, the compiler will default to "thiscall" calling convention and will pass this in the ecx register. If you specify __stdcall for you member function, it will be pushed on the stack as the first parameter.

有关VC ++ 64位,前四个参数在寄存器中传递。这是第一个参数,并在RCX寄存器传递。

For x64 on VC++, the first four parameters are passed in registers. This is the first parameter and passed in the rcx register.

雷蒙德陈了一系列几年前就调用约定。这里有 86 并的x64 文章。

Raymond Chen had a series some years ago on calling conventions. Here are the x86 and x64 articles.

这篇关于C ++的隐含这一点,它究竟是如何压入堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆