Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the 'this' pointer stored in computer memory?

Where exactly is the 'this' pointer stored in memory? Is it allocated on the stack, in the heap, or in the data segment?

#include <iostream> using namespace std;  class ClassA {     int a, b;      public:         void add()         {             a = 10;             b = 20;             cout << a << b << endl;         } };  int main() {     ClassA obj;     obj.add();     return 0; } 

In the above code I am calling the member function add() and the receiver object is passed implicitly as the 'this' pointer. Where is this stored in memory?

like image 351
nagaradderKantesh Avatar asked May 16 '13 10:05

nagaradderKantesh


People also ask

Where is the pointer stored in memory?

To answer your question: ptr is stored at stack.

What is pointer in memory?

Stated simply, a pointer is nothing more than a variable that holds an address in the computer's memory. This is where a pointer gets its name. A pointer variable holds the address of a certain piece of memory in the computer; in other words, a pointer points at a specific location in memory.

Is a pointer stored in its own memory address?

Yes, a declared pointer has its own location in memory.

Does a pointer take up memory?

Pointer itself takes up space in the memory and can be pointed only to the data type you specify while creating that pointer. So yes even if you have nothing assigned to it, pointer will take memory space.


2 Answers

The easiest way is to think of this as being a hidden extra argument that is always passed automatically.

So, a fictional method like:

size_t String::length(void) const {   return strlen(m_string); } 

is actually more like this under the hood:

size_t String__length(const String *this) {   return strlen(this->m_string); } 

and a call like:

{   String example("hello");   cout << example.length(); } 

becomes something like:

cout << String__length(&example); 

Note that the above transformation is simplified, hopefully to make my point a bit clearer. No need to fill up the comments with "whaaa, where's the marshalling for method overloading, huh?"-type objection, please. :)

That transforms the question into "where are arguments stored?", and the answer is of course "it depends". :)

It's often on the stack, but it could be in registers too, or any other mechanism that the compiler considers is good for the target architecture.

like image 161
unwind Avatar answered Oct 13 '22 16:10

unwind


Other answers have done a very good job explaining how a typical compiler implements this (by passing it as an implicit first parameter to the function).

I think it's also useful to see what the C++ ISO spec explicitly says about this. According to the C++03 ISO spec, §9.3.2/1:

In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called.

It's important to note that this is not a variable - it's an expression, much in the same way that the expression 1 + 2 * 3 is an expression. The value of this expression is permitted to be stored pretty much anywhere. The compiler might put it on the stack and pass it as an implicit parameter to a function, or it might put it in a register, and it conceivably could put it in the heap or in the data segment. The C++ specification deliberately gives the implementation some flexibility here.

I think that the "language-lawyer" answer is "this is completely implementation-defined, and moreover this is technically not a pointer, but an expression that evaluates to a pointer."

Hope this helps!

like image 41
templatetypedef Avatar answered Oct 13 '22 16:10

templatetypedef