In C++, this pointer get passed to method as a hidden argument which actually points to current object, but where 'this' pointer stored in memory... in stack, heap, data where?
-
Assuming you mean how does your code know where `this` is when a method is invoked ... It's going to be different depending on a number of factors, but surely you can take a guess or look at the assembly your compiler generates to get an idea. – asveikau Jan 12 '14 at 03:49
-
i read that 'this' is not pointer actually, its an expression, is it true? – nilesh Jan 12 '14 at 03:54
-
exact duplicate of [Where is the 'this' pointer stored in computer memory?](http://stackoverflow.com/questions/16585562/where-is-the-this-pointer-stored-in-computer-memory) – underscore_d May 21 '17 at 12:52
4 Answers
The standard doesn't specify where the this
pointer is stored.
When it's passed to a member function in a call of that function, some compilers pass it in a register, and others pass it on the stack. It can also depend on the compiler options.
About the only thing you can be sure of is that this
is an rvalue of basic type, so you can't take its address.
It wasn't always that way.
In pre-standard C++ you could assign to this
, e.g. in order to indicate constructor failure. This was before exceptions were introduced. The modern standard way of indicating construction failure is to throw an exception, which guarantees an orderly cleanup (if not foiled by the user's code, such as the infamous MFC placement new
bug).

- 142,714
- 15
- 209
- 331
In C++, this
is "simply" a pointer to the current object. It allows you to access object-specific data.
For example, when code in a class has the following snippet:
this->temperature = 40.0f;
it sets the temperature for whatever object is being acted upon (assuming temperature
is not a class-level static, shared amongst all objects of the class).
The this
pointer itself doesn't have to be allocated (in terms of dynamic memory), it depends entirely on how it's all handled under the covers, something the standard doesn't actually mandate (the standard tends to focus more on behaviour than internals).
There are any number of places it could be: on the stack, at a specific memory location, in a register, and so on. All you have to concern yourself with is its behaviour which is basically how you use it to get access to the object.
What this
points to is the object itself and it's usually allocated with new
for dynamic allocation, or on the stack.

- 854,327
- 234
- 1,573
- 1,953
The this pointer is allocated on the stack of your class functions (or sometimes a register).
This is however not likely the question you are actually asking.

- 40,822
- 8
- 72
- 132
-
1Actually it appears to be exactly the question that is being asked. I'm afraid the answer is too vague for an upvote though. – Mark Ransom Jan 12 '14 at 03:49
The this pointer is in the object itself*. Sort of. It is the memory location of the object.
If the object is on the stack, the this pointer is on the stack.
If the object is on the heap, the this pointer is on the heap.
Bottom line, it's nothing you need to worry about.
*[UPDATE] Let me backpedal/clarify/correct my answer. The physical this pointer is not in the object.
I would conclude the this pointer is derived by the compiler and is simply the address of the object which is stored in the symbol table. Semantically, it is inside the object but that was not what the OP was asking.
Here is the memory layout of 3 variables on the stack. The middle one is an object. You can see it holds it's one variable and nothing else:

- 20,506
- 2
- 28
- 69
-
then if we calculate size of object then we should get actual size + size of this pointer i.e. 4 bytes extra.. but this is not the case – nilesh Jan 12 '14 at 03:51
-
I don't think it shows up in the sizeof operator. The pointer can be different sizes depending on the compiler. If you have virtual functions, there is also a hidden table of function pointers that do not show up in the sizeof operator. – Steve Wellens Jan 12 '14 at 03:53
-
but we can see extra 4 byte allocated for virtual pointer which points to vtable – nilesh Jan 12 '14 at 03:55
-
The reason I say the this pointer is inside the object...can you access it from outside the object? – Steve Wellens Jan 12 '14 at 05:15
-
-
2It's not really correct to say it's inside the object itself. It's not a member variable at all. Saying that it's on the heap if the object is on the heap is just plain wrong. – TheUndeadFish Jan 12 '14 at 06:40
-
@TheUndeadFish - In my original answer I did say, "Sort of." If my answer isn't correct, what IS the correct answer? – Steve Wellens Jan 12 '14 at 13:57
-
The address at which a pointer is stored is not the same thing as its value, i.e. the address to which it points. And the `this` pointer is (obviously) not a thing that permanently exists; it's just passed around, so it will be passed however the ABI dictates. – underscore_d May 21 '17 at 12:55