Memory Management from C++11 encompasses the way memory is dynamically allocated and unallocated through, mainly, the
new
anddelete
operators. Beyond, C++ makes on its own many data copies to exchange data between running scopes. For example,return
statements copy data from callees to callers. As a result, the way data burdens memory requires developer control and clairvoyance as well.Robust (sober) programming involves canonical and systematic support (e.g., best practice) in classes, which deal with dynamical memory: support for initialization, assignment, move…
default
and delete
keywordsstd::string_view
(C++17)Rule(s)
- In C++, initialization and assignment are radically different. Initialization relies on the copy constructor while assignment is the possible redefinition of the
=
operator.- Resource Acquisition Is Initialization -RAII- here… is the principle that object creation is the appropriate time for acquiring resources (dynamic memory for instance through
new
). Robust programming involves the release ot these resources at destruction time.Example Human_being.Cpp.zip
![]()
“rvalue” versus “lvalue” “Move” semantics
Rule(s)
- C++11 introduces the idea of “rvalue” opposite to that of “lvalue”. Formally, in C++14, C++17 and, C++20, it exists the distinction between pure “rvalue” (a.k.a. “prvalue”), “lvalue”, “xvalue”… Further detail here…
- To simplify, a “lvalue” is an expression that refers to a memory location and allows us to take the address of that memory location via the
&
operator. In contrast, until C++11, a “rvalue” was not programmatically accessible even though it relates to some memory space.- C++11 changes the deal as follows: if
X
is any type thenX&&
is called a “rvalue” reference toX
.Example (foundation) Rvalue.Cpp.zip
![]()
Example (bypass copy) Rvalue.Cpp.zip
![]()
Rule(s)
- “Move” semantics may be forced…
Example (force move) Rvalue.Cpp.zip
![]()
Rule(s)
- Things that are declared as “rvalue” reference can be a “lvalue” or a “rvalue”. The distinguishing criterion is: if it has a name then it is a “lvalue” else it is a “rvalue”.
Example Rvalue.Cpp.zip
![]()
Perfect forwarding
Rule(s)
- The definition of classes may refer to not-yet-defined classes: forwarding. This is especially true and crucial for template classes that deal with anonymous types, but implicitly require key characteristics owned by these types.
Example (forwarding, reminder)
Rule(s)
std::forward
(here…) is a key C++ facility (based onstd::remove_reference
) to adapt between “lvalue” and “rvalue”. Collapsing rules fromstd::remove_reference
are also described here…Example (adapt between “lvalue” and “rvalue”: failure) Rvalue.Cpp.zip
![]()
Rule(s)
std::forward
(here…) relies on type deduction (a.k.a. “collapsing rules”) from template instantiation. A template-based version ofGetClone
is therefore required.Example (adapt between “lvalue” and “rvalue”: success) Rvalue.Cpp.zip
![]()
See also
The default
anddelete
keywords may, from C++11, be associated with “implicit” functions: constructors (base, copy, move), destructor and assignment (copy, move)Rule(s)
- Triviality like TriviallyCopyable is the general-purpose mechanism for assigning/copying/moving an object to another. Triviality is lost as soon as the type of interest, e.g.,
My_class
, has, for instance, a resource attribute to be assigned/copied/moved, e.g.,std::string _resource;
, which is itself not trivially copyable.- Beyond, if the type of interest is polymorphic (through its destructor, for instance) then
std::is_polymorphic<My_class>
⇒ !std::is_trivially_copyable<My_class>
, !std::is_trivially_assignable<My_class>
, and !std::is_trivially_move_constructible<My_class>
.Example Default_delete.Cpp.zip
![]()
Example (stupid people?)
std::string_view
Rule(s)
std::string_view
(#include <string_view>
) objects are lightweight objects that refer tostd::string
objects.- Principle:
std::string
objects have to persist in memory. For example, extracting a sub-string may be viewed as creating a newstd::string
object: the extracted memory part (old school) or referring to memory part embodying the extraction (std::string_view
object). Of course, a key point is the “stability” in memory of the initialstd::string
object.Example (load CSV file into memory) GTIN.Cpp.zip
![]()
Note(s)
- Lines 10-13 and 43 are an example of native Observer Model-View-Controller -MVC- design patterns in C++
Exercise
- Replace copy by move (lines 52-53)