Is it possible to obtain the address of the 'this' pointer?

Viewed 3028

I read that this is an rvalue and we cannot get its address by applying &this.

In my code, I have tried to use a reference binding to this. I'm wondering which way will give the address of this? Or are both wrong?

What exactly is this? An lvalue, an rvalue, a keyword, or something else?

void MyString::test_this() const{
    std::cout << "this: " << this << std::endl;
    const MyString * const& this_ref = this;
    std::cout << "thie_ref: " << &this_ref << std::endl;
    const MyString * const&& this_right = this;
    std::cout << "thie_right: " << &this_right << std::endl;
}

//this: 00CFFC14
//thie_ref: 00CFFB14
//thie_right: 00CFFAFC
4 Answers

I'm wondering which may give the address of this? Or both are wrong?

Neither is the address of this, because the C++ abstract machine doesn't define an address for it. this is like 0. You can't get the address of 0, it's not an entity with storage, just some value. So what does this do?

int const& i = 0;

It creates a temporary object, initializes it with 0, and then binds the reference to it. The same exact thing occurs in your code. You create references to different temporary objects that hold the value of this.

this is a keyword that stands for the address of the object that the member function is being executed for. The C++ abstract machine doesn't require it to occupy storage, so it's always (logically) just a plain value, like 0.

There's merit to not requiring this to occupy storage. It allows C++ to be implemented over an ABI where this is passed in a register (something that isn't addressable normally). If &this had to be well-defined, i.e. if this had to be addressable, it would preclude an implementation from using a register for passing the address. The C++ standard generally aims not to tie implementations up like that.

What is meant by "you cannot take the address of this" is, that you cannot write &this.

You are asking for the address represented by the this pointer, right? This is, what your first output does.

this itself is not materialized as a pointer, like e.g. MyString* x would be. Here, x itself has a location in memory and you can do sth. like &x. This is not possible for this.

The answer to your last question is: yes, this is a keyword. The expression this is a primary expression. You can read about it in Section [expr.prim.this] of the C++ standard.

this is a pointer containing the address to the current object. It is not a variable that is stored somewhere (or could even be changed), it is a special keyword with special properties.

If you want to know the address of the "current object" you can simply output as shown below in the program:

#include<iostream>

using namespace std;

class Test
{
    public:
            void fun()
            {
                    cout << "Address of this :: " << this << endl;
                    void *ptr = this;
                    cout << "Addrss of ptr :: " << ptr << endl;
            }
 };

 int main()
 {
    Test obj;

    cout << "Address of obj :: " << &obj << endl;

    obj.fun();

    return 0;
 }

Above program produces below output:

Address of obj :: 0x7fffef913627

Address of this :: 0x7fffef913627

Addrss of ptr :: 0x7fffef913627

I hope it helps!

The this keyword behaves essentially as &__self where __self is a lvalue designating the object on which the member function is run (the *this object) except that an overloaded operator&() isn't used.

So &this would mean &&__self which obviously doesn't make sense, you can't take the address of an address, just like you can't take the address of a scalar return value of a function:

int fi(); 
int *fpi(); 

Both &(fi()) and &(fpi()) are illegal as the rvalue returned is a scalar (probably stored in a register) and as a pure scalar value does not have an address.

Related