Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the value of `this` pointer constant during the object's lifetime?

Is the value of this pointer guaranteed to be constant during a lifetime of a particular object? I can't imagine a case where it would change, but don't know whether I am not missing something.

like image 732
Daniel Langr Avatar asked Jan 15 '20 10:01

Daniel Langr


People also ask

What is object lifetime in c++?

The lifetime of an object begins when its initialization is complete, and ends when its storage is released. Dynamic storage duration starts when the storage created by (new Type) is initialized, and ends when the object goes out of scope or is deleted by “delete pointer”.

What is the life time of local object?

The lifetime of a variable is the time during which the variable stays in memory and is therefore accessible during program execution. The variables that are local to a method are created the moment the method is activated (exactly as formal parameters) and are destroyed when the activation of the method terminates.

What happens to a pointer when you free it?

The function free takes a pointer as parameter and deallocates the memory region pointed to by that pointer. The memory region passed to free must be previously allocated with calloc , malloc or realloc . If the pointer is NULL , no action is taken.

What does the this pointer always point at?

The this pointer holds the address of current object, in simple words you can say that this pointer points to the current object of the class.


2 Answers

Is the value of this pointer guaranteed to be constant during a lifetime of a particular object?

Yes.

As user Aconcagua puts it: the value of this pointer always is the value of the address of the object on which the function was called on1. So the question is equivalent with:

Can an object change its memory address over life time?

This is not possible, by definition of lifetime2. The lifetime of an object begins when or after its storage is obtained and ends before of when it is released.


1)[class.this]/1

In the body of a non-static ([class.mfct]) member function, the keyword this is a prvalue whose value is a pointer to the object for which the function is called.

2)[basic.life]/1 (emphasis mine)

The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and, if it is of class type or a (possibly multi-dimensional) array thereof, that class type has a trivial default constructor. The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete (including vacuous initialization) ([dcl.init]), except that if the object is a union member or subobject thereof, its lifetime only begins if that union member is the initialized member in the union ([dcl.init.aggr], [class.base.init]), or as described in [class.union].

The lifetime of an object o of type T ends when:

  • if T is a non-class type, the object is destroyed, or
  • if T is a class type, the destructor call starts, or
  • the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]).
like image 81
YSC Avatar answered Oct 11 '22 22:10

YSC


An object has a region of storage. this points there.

[intro.object]/1

An object occupies a region of storage in its period of construction ([class.cdtor]), throughout its lifetime, and in its period of destruction ([class.cdtor]).

like image 26
Caleth Avatar answered Oct 12 '22 00:10

Caleth