The C++ implicit this, and exactly how it is pushed on the stack
assembly, c++, pointers, stack, this
Solution
This depends on the calling convention of your compiler and the target architecture.
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.
For x64 on VC++, the first four parameters are passed in registers. This is the first parameter and passed in the rcx register.
Raymond Chen had a series some years ago on calling conventions. Here are the x86 and x64 articles.
Problem
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); ``` 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. 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. EDIT: to clarify things, I'm using GCC/G++ 4.3