MarketSmart pointer
Company Profile

Smart pointer

In computer science, a smart pointer is an abstract data type that simulates a pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bugs caused by the misuse of pointers, while retaining efficiency. Smart pointers typically keep track of the memory they point to, and may also be used to manage other resources, such as network connections and file handles. Smart pointers were first popularized in the programming language C++ during the first half of the 1990s as rebuttal to criticisms of C++'s lack of automatic garbage collection. Rust, which avoids raw pointers and uses ownership to dictate lifetimes, also has smart pointers.

History
Even though C++ popularized the concept of smart pointers, especially the reference-counted variety, the immediate predecessor of one of the languages that inspired C++'s design had reference-counted references built into the language. C++ was inspired in part by Simula67. Simula67's ancestor was Simula I. Insofar as Simula I's element is analogous to C++'s pointer without null, and insofar as Simula I's process with a dummy-statement as its activity body is analogous to C++'s struct (which itself is analogous to C. A. R. Hoare's record in then-contemporary 1960s work), Simula I had reference counted elements (i.e., pointer-expressions that house indirection) to processes (i.e., records) no later than September 1965, as shown in the quoted paragraphs below. Processes can be referenced individually. Physically, a process reference is a pointer to an area of memory containing the data local to the process and some additional information defining its current state of execution. However, for reasons stated in the Section 2.2 process references are always indirect, through items called elements. Formally a reference to a process is the value of an expression of type element. … element values can be stored and retrieved by assignments and references to element variables and by other means. The language contains a mechanism for making the attributes of a process accessible from the outside, i.e., from within other processes. This is called remote accessing. A process is thus a referenceable data structure. It is worth noticing the similarity between a process whose activity body is a dummy statement, and the record concept recently proposed by C. A. R. Hoare and N. Wirth Because C++ borrowed Simula's approach to memory allocationthe new keyword when allocating a process/record to obtain a fresh element to that process/recordit is not surprising that C++ eventually resurrected Simula's reference-counted smart-pointer mechanism within element as well. == Features ==
Features
In C++, a smart pointer is implemented as a template class that mimics, by means of operator overloading, the behaviors of a traditional (raw) pointer, (e.g. dereferencing, assignment) while providing additional memory management features. Smart pointers can facilitate intentional programming by expressing, in the type, how the memory of the referent of the pointer will be managed. For example, if a C++ function returns a pointer, there is no way to know whether the caller should delete the memory of the referent when the caller is finished with the information. SomeType* ambiguousFunction(); // What should be done with the result? Traditionally, naming conventions have been used to resolve the ambiguity, which is an error-prone, labor-intensive approach. C++11 introduced a way to ensure correct memory management in this case by declaring the function to return a std::unique_ptr, unique_ptr obviousFunction(); The declaration of the function return type as a unique_ptr makes explicit the fact that the caller takes ownership of the result, and the C++ runtime ensures that the memory will be reclaimed automatically. Before C++11, unique_ptr can be replaced with , which is now deprecated. == Creating new objects ==
Creating new objects
To ease the allocation of a std::shared_ptr, C++11 introduced: shared_ptr s = std::make_shared(constructor, parameters, here); and similarly std::unique_ptr, since C++14 one can use: unique_ptr u = std::make_unique(constructor, parameters, here); It is preferred, in almost all circumstances, to use these facilities over the new keyword. == Unique pointers ==
Unique pointers
C++11 introduces , defined in the header . In previous versions of Rust, there was also a that wrapped around raw non-null . It is possible to use "smart void pointers" using std::unique_ptr, using its second template parameter Deleter, which stores a type of a deallocator. using std::unique_ptr; struct MyDeleter { void operator()(void* p) const { delete static_cast(p); } }; int main() { unique_ptr myPointer(new int(42), MyDeleter()); } == Shared pointers and weak pointers ==
Shared pointers and weak pointers
C++11 introduces and , defined in the header . == Hazard pointers ==
Hazard pointers
Starting in C++26, there is a new pointer defined in , the hazard pointer (). It is a single-writer multi-reader pointer that can be owned by at most one thread at any point in time. The hazard pointer had existed already previously in some third-party libraries. == Other types of smart pointers ==
Other types of smart pointers
There are other types of smart pointers (which are not in the C++ standard) implemented on popular C++ libraries or custom STL, some examples include the intrusive pointer. == See also ==
tickerdossier.comtickerdossier.substack.com