Skip to main content

Object Lifetime and Storage Duration


Introduction:
While creating an object, its location in memory has to be established, before it is initialized. Initialization means putting value into the location. The lifetime of an object starts just after initialization. When an object dies, its location (storage), which the object occupied is released and then the computer is shut down or the storage is taken up (used) by another object. Releasing a storage means, making the identifier or pointer that occupied the storage, invalid. The lifetime of an object ends, when its storage is released. Some time is needed to create an object. Some time is needed to kill an object. When talking about an object, two things are involved: the location which is the storage, and the value. The meaning of lifetime and storage duration are similar; but the duration is seen more from the point of view of the location than from the point of view of the value. The storage duration is the time from when a location is associated to an object to the time when the location is dissociated from the object. Storage duration is determined by one of the following schemes: automatic storage duration; dynamic storage duration; static storage duration; thread storage duration.

Let’s look at the types of storage durations.

Automatic storage duration
 A local variable declared at block scope normally has an automatic storage duration. Local variables are stored in a run-time stack. Allocating storage for local variables usually takes just one machine instruction. Each time a function is called, a stack frame (a block of memory in the stack) is allocated for the function’s local variables, and the stack frame is deallocated when the function returns. Thus, for a variable with an automatic storage duration, the lifetime of its storage begins upon entry into the block immediately enclosing the object’s declaration and ends upon exit from the block.

Dynamic storage duration
A local variable declared at block scope can have a dynamic storage duration if its storage is allocated by calling an allocation function, such as malloc() in C or the operator new in C++. Dynamic memory allocation allows a user to manage memory very economically. The drawback is that it is much slower than automatic allocation because it typically involves tens or hundreds of instructions. For a variable with a dynamic storage duration, the lifetime of its storage lasts until the memory is deallocated explicitly—say, by a free function in C or the operator delete in C++.

Static storage duration
For a variable with a static storage duration, its storage size and address are determined at compile time (before the program starts running); the lifetime of its storage is the entire program execution time. A variable declared at file/namespace scope has a static storage duration. 

Thread storage duration
All variables declared with the “thread_local” keyword have thread storage duration. The storage for these entities shall last for the duration of the thread in which they are created. There is a distinct object or reference per thread, and use of the declared name refers to the entity associated with the current thread. A variable with thread storage duration shall be initialized before its first odr-use (One Definition Rule) and, if constructed, shall be destroyed on thread exit.

Conclusion:
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 duration of a static object begins when the object has been initialized, and ends at the end of the program. Once a static object has been initialized, its initial value cannot be changed, even if its definition is re-evaluated. Storage duration of objects and references plays a very important role in writing programs. The proper use of storage of objects and references allows better memory management and in turn allows us to write better code.

Comments