C ++ implies this, and exactly how it is pushed on the stack

I need to know if calling the class method in C ++ will be the implicit pointer to 'this' as the first argument or the last. ie: first or last pushed the stack.

In other words, I ask if the method of the class that is being called is a compiler:

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

Thus, and, more importantly, it would also answer whether g ++ would indicate this pointer last or first, respectively. I interrogated Google, but I did not find much.

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 

In general: is the class method called this way?

 ; 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 very grateful.

EDIT: to clarify the situation, I am using GCC / g ++ 4.3

+6
c ++ assembly stack pointers this
source share
4 answers

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

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

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

Raymond Chen had a series of convention calls a few years ago. The following are x86 and x64 articles.

+15
source share

It will depend on your compiler and architecture, but in g ++ 4.1.2 on Linux without optimization settings, it treats this as the first parameter passed in the register:

 class A { public: void Hello(int, int) {} }; void Hello(A *a, int, int) {} int main() { A a; Hello(&a, 0, 0); a.Hello(0, 0); return 0; } 

Dismantling main ():

 movl $0, 8(%esp) movl $0, 4(%esp) leal -5(%ebp), %eax movl %eax, (%esp) call _Z5HelloP1Aii movl $0, 8(%esp) movl $0, 4(%esp) leal -5(%ebp), %eax movl %eax, (%esp) call _ZN1A5HelloEii 
+8
source share

I just read the C ++ Standard (ANSI ISO IEC 14882 2003), section 9.3.2 "This Pointer", and it does not seem to indicate anything about where this should happen in the argument list, so this is up to the individual compiler.

Try compiling some code with gcc using the '-S' flag to generate assembly code and see what it does.

+2
source share

This detailed information is not specified by the C ++ standard. However, read the C ++ ABI for gcc (and other C ++ compilers that follow the C ++ ABI).

+1
source share

All Articles