Others
Log2
A utility function to find Log n in base 2
int log2(int n) {
return (n==1)? 0 : 1 + log2(n/2);
}
RAII (Resource Acquisition Is Initialization)
Resource acquisition is initialization (RAII)is a programming idiom used in several object-oriented languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tied to object lifetime: resource allocation (or acquisition) is done during object creation (specifically initialization), by the constructor, while resource deallocation (release) is done during object destruction (specifically finalization), by the destructor. In other words, resource acquisition must succeed for initialization to succeed. Thus the resource is guaranteed to be held between when initialization finishes and finalization starts (holding the resources is a class invariant), and to be held only when the object is alive. Thus if there are no object leaks, there are no resource leaks.
RAII is associated most prominently with C++ where it originated, but also D, Ada, Vala, and Rust. The technique was developed for exception-saferesource management in C++during 1984--89, primarily by Bjarne Stroustrup and Andrew Koenig, and the term itself was coined by Stroustrup.RAII is generally pronounced as an initialism, sometimes pronounced as "R, A, double I".
Other names for this idiom includeConstructor Acquires, Destructor Releases(CADRe)and one particular style of use is calledScope-based Resource Management(SBRM).This latter term is for the special case of automatic variables. RAII ties resources to objectlifetime, which may not coincide with entry and exit of a scope. (Notably variables allocated on the free store have lifetimes unrelated to any given scope.) However, using RAII for automatic variables (SBRM) is the most common use case.
https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization
Resource Acquisition Is Initializationor RAII, is a C++ programming techniquewhich binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection - anything that exists in limited supply) to the lifetime of an object.
RAII guarantees that the resource is available to any function that may access the object (resource availability is a class invariant, eliminating redundant runtime tests). It also guarantees that all resources are released when the lifetime of their controlling object ends, in reverse order of acquisition. Likewise, if resource acquisition fails (the constructor exits with an exception), all resources acquired by every fully-constructed member and base subobject are released in reverse order of initialization. This leverages the core language features (object lifetime, scope exit, order of initialization and stack unwinding) to eliminate resource leaks and guarantee exception safety. Another name for this technique isScope-Bound Resource Management(SBRM), after the basic use case where the lifetime of an RAII object ends due to scope exit.
RAII can be summarized as follows:
- encapsulate each resource into a class, where
- the constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done,
- the destructor releases the resource and never throws exceptions;
- always use the resource via an instance of a RAII-class that either
- has automatic storage duration or temporary lifetime itself, or
- has lifetime that is bounded by the lifetime of an automatic or temporary object
Move semantics make it possible to safely transfer resource ownership between objects, across scopes, and in and out of threads, while maintaining resource safety.
Classes with open()/close(), lock()/unlock(), or init()/copyFrom()/destroy() member functions are typical examples of non-RAII classes:
https://en.cppreference.com/w/cpp/language/raii
Array Decay
The loss of type and dimensions of an array is known as decay of an array.This generally occurs when we pass the array into function by value or pointer. What it does is, it sends first address to the array which is a pointer, hence the size of array is not the original one, but the one occupied by the pointer in the memory.
https://www.geeksforgeeks.org/what-is-array-decay-in-c-how-can-it-be-prevented