Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 357
Range Hao Avatar asked Jul 01 '19 09:07

Range Hao


People also ask

How do I find my pointer address?

By the help of * (indirection operator), we can print the value of pointer variable p. Let's see the pointer example as explained for the above figure. printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.

Can you get the address of a null pointer?

Memory address 0 is called the null pointer. Your program is never allowed to look at or store anything into memory address 0, so the null pointer is a way of saying "a pointer to nothing". Note that a null pointer is not the same as a null character; do not confuse the two.

How do you print the address of a pointer in C?

You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.

Can we store the address of a pointer in another pointer?

Yes, but it needs to have the right type. In your example int *ptr,*ptr1; both ptr and ptr1 have type "pointer to int ", which can only point to an int , not a pointer. If you declare int *ptr, **ptr1; , then ptr1 has type "pointer to int * " and thus can point to ptr . Save this answer.


1 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.

like image 64
StoryTeller - Unslander Monica Avatar answered Sep 26 '22 02:09

StoryTeller - Unslander Monica