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.
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”.
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.
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.
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.
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 lifetime
2. 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 keywordthis
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 typeT
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]
).
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]
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With