Title: Theory / Unimportant / CS / Programming / C++ / Manual / IV STL / 34 Memory and ressources;
Tags: automatedNewLineAfterCommaSpace

Instruction: std::unique_ptr<Foo> (value)
Page: 120, 988
Effect: A unique_pointer with content value
Language: C++
Chapter: 4, 34
Section: 18, 4.1

Typ: std::weak_ptr<T>
Meaning: Similar to std::shared_ptr<T>, except that it do not take ownership; it becomes a null pointer when no shared_ptr point to it
Page: 138, 993
Language: C++
Chapter: 4, 34
Section: 20, 3.3
Implementation: pointer to a shared pointer but not counted in this shared pointer count

Instruction: std::make_shared<T>(args)
Page: 135, 992
Language: C++
Chapter: 4, 34
Section: 21, 3.2
Returns: A shared_ptr to `new T(args)`
Assuming: c++ at least 11

Instruction: array<T, n> {
expr_1,
...,
expr_m
}
Page: 975
Language: c++
Context: stl
Chapter: 34
Section: 2.1
Returns: an array with n elements, m first one initialized with the expr_i, the one between m+1 and n with default initializer

Instruction: arr.fill(value)
Page: 975
Effect: fill the array with value
Language: c++
Context: stl
Chapter: 34
Section: 2.1
Variables: Array<T, n> arr

Instruction: get<i>(arr)
Page: 975
Language: c++
Context: stl::array
Chapter: 34
Section: 2.1
Returns: the i-th element of arr
Variables: Array<T, n> arr

Instruction: Tuple_size(decltype(arr))
Page: 975
Language: c++
Context: stl
Chapter: 34
Section: 2.1
Returns: the size of the array, at compilation time
Variables: Array<T, n> arr

Instruction: bitset {n}
Page: 978
Language: c++
Context: stl
Chapter: 34
Section: 2.2.1
Returns: a bitset with bits as indicated in the the number n

Instruction: bitset {args, z, o}
Page: 978
Language: c++
Context: stl
Chapter: 34
Section: 2.2.1
Returns: Same as bitset {args}, with z, o for zero and one

Instruction: bitset {args, n}
Page: 978
Language: c++
Context: stl
Chapter: 34
Section: 2.2.1
Returns: same as bitset {args}, limited to n bits

Instruction: bitset {s, i}
Page: 978
Language: c++
Context: stl
Chapter: 34
Section: 2.2.1
Returns: a bitset, where bits are read from string s starting at bit i

Instruction: bitset {s}
Page: 978
Language: c++
Context: stl
Chapter: 34
Section: 2.2.1
Returns: bitset {s, 0}

Instruction: bs[i]
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: the i-th bit of bs
Variables: biset<n> bs

Instruction: bs.test(i)
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: the i-th bit of bs
Variables: biset<n> bs
Example: out_of_range if i is not in [0: bs.size())

Instruction: bs & bs2
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: the bitwise and of the two bitsets
Variables: biset<n> bs bs2

Instruction: bs | bs2
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: the bitwise or of the two bitsets
Variables: biset<n> bs bs2

Instruction: bs ^ bs2
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: the bitwise exclusive or of the two bitsets
Variables: biset<n> bs bs2

Instruction: bs << i
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: logical left-shifted bs by i bits
Variables: biset<n> bs

Instruction: bs >> i
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: logical right-shifted bs by i bits
Variables: biset<n> bs

Instruction: ~bs
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: complement set of bs
Variables: biset<n> bs

Instruction: bs.set()
Page: 979
Effect: set every bits to 1
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs

Instruction: bs.set(
  i,
  v
)
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs
Abbreviation: bs[i] = v

Instruction: bs.reset()
Page: 979
Effect: Set every bit to 0
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs

Instruction: bs.reset(i)
Page: 979
Effect: bs[i] =0
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs

Instruction: bs.flip(i)
Page: 979
Effect: bs[i] =~bs[i]
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs

Instruction: bs.count()
Page: 979
Effect: The number of bits with value 1 in bs
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs

Instruction: bs.size()
Page: 979
Effect: The number of bits in bs
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Variables: biset<n> bs

Instruction: bs.all()
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: are all bit one
Variables: biset<n> bs

Instruction: bs.any()
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: are some bit one
Variables: biset<n> bs

Instruction: bs.none()
Page: 979
Language: c++
Context: stl
Chapter: 34
Section: 2.2.2
Returns: are no bit one
Variables: biset<n> bs

Question: What are the difference between bitset and vector<bool>
Answer: bitset has fixed size
Page: 982
Chapter: 34
Section: 2.3
Context: c++
Answer2: vector<bool> can take an allocator

Typ: pair<T, U>
Meaning: A pair with first element of type T and second of type U
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1

Typ: p.first/second_type
Meaning: T_1/T_2
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1
Variables: pair<T_1, T_2> p

Instruction: p.first/p.second
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1
Returns: the first/second element of p
Variables: pair<T_1, T_2> p

Instruction: pair<T_1, T_2> {piecewise_construct, tuple_1, tuple_2}
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1
Returns: a pair, with the left element using constructor tuple_1 and second element using constructor tuple_2
Extra: piecewise_construct is literall. A constant for disambiguation

Instruction: p1<p2
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1
Returns: p1.first < p2.first (p1.first == p2.first && p1.second < p2.second)
Variables: pair<T1, U1> p1
pair<T2, U2> p2

Instruction: make_pair(
  x,
  y
)
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1
Returns: a pair holding the value x, y

Instruction: tuple_element<N, T>::type
Page: 982
Language: C++
Chapter: 34
Section: 2.4.1
Returns: the type of the N-th value of T
Assuming: T tuple/pair

Instruction: tuple {}
Page: 982
Language: C++
Chapter: 34
Section: 2.4.2
Returns: the empty tuple
Assuming: T tuple/pair

Instruction: tuple {arg_1, ..., arg_n}
Page: 982
Language: C++
Chapter: 34
Section: 2.4.2
Returns: the tuple with types decltype(arg_1), ..., decltype(arg_n) and values arg_1, ..., arg_n
Assuming: not a single arg which is a tuple/pair. Otherwise copy/move operator

Typ: tuple<type_1, ..., type_n>
Meaning: a container with n elements. I-th element of type type_i
Page: 982
Language: C++
Chapter: 34
Section: 2.4.2

Instruction: make_tuple(args)
Page: 982
Language: c++
Chapter: 34
Section: 2.4.2
Returns: a tuple with values of args

Instruction: forward_as_tuple(args)
Page: 982
Language: c++
Chapter: 34
Section: 2.4.2
Returns: a tuple with values of args, which is a rvalue reference

Instruction: tie(
  var_0|ignore,
  ...,
  var_n|ignore
) = t
Page: 986
Language: c++
Chapter: 34
Section: 2.4.2
Returns: assign get<0>(  t) to var_0, ..., get<n>(  t) to var_n. except for the var_i which are ignore

Instruction: tuple_cat(
  tuple_1,
  ...,
  tuple_n
)
Page: 982
Language: c++
Chapter: 34
Section: 2.4.2
Returns: the tuple which is the concatenation of tuple_1 ... and tuple_n

Instruction: *up
Page: 988
Language: c++
Chapter: 34
Section: 3.1
Returns: the value pointed by up
Variables: unique_ptr<T> up
Assuming: T is not an array

Instruction: up->m
Page: 988
Language: c++
Chapter: 34
Section: 3.1
Returns: the member m of the value pointed by up
Variables: unique_ptr<T> up
Assuming: T is not an array

Instruction: up[i]
Page: 988
Language: c++
Chapter: 34
Section: 3.1
Returns: the i-th value of the array pointed by up
Variables: unique_ptr<T> up

Instruction: up.get()
Page: 988
Language: c++
Chapter: 34
Section: 3.1
Returns: the contained pointer
Variables: unique_ptr<T> up

Instruction: up.get_deleter()
Page: 988
Language: c++
Chapter: 34
Section: 3.1
Returns: the deleter of up
Variables: unique_ptr<T> up

Instruction: up.release()
Page: 988
Effect: set the contained pointer to nullptr
Language: c++
Chapter: 34
Section: 3.1
Returns: the contained pointer
Variables: unique_ptr<T> up

Instruction: up.reset()
Page: 988
Effect: call deleter for current value. Give the value T{}
Language: c++
Chapter: 34
Section: 3.1
Variables: unique_ptr<T> up

Instruction: up.reset(p)
Page: 988
Effect: call deleter for current value. Give the value p
Language: c++
Chapter: 34
Section: 3.1
Variables: unique_ptr<T> up

Instruction: up1 < up2
Page: 988
Language: c++
Chapter: 34
Section: 3.1
Returns: Whether the contained pointer of up1 is less than the one of up2
Variables: unique_ptr<T1> up1
unique_ptr<T2> up2

Instruction: make_unique<T>(args)
Page: 990
Effect: Generate a unique pointer initialized with T{args}
Language: c++
Chapter: 34
Section: 3.1

Typ: sthared_ptr<T>
Meaning: A pointer to an element of type T that can be copied. When no copy remain, the object pointed to is deleted
Page: 990
Language: c++
Chapter: 34
Section: 3.1

Question: What are the risks with shared pointer
Answer: In case of loop in the data structure, data leak can occur as the loop is never deleted
Page: 990
Chapter: 34
Section: 3.1
Context: C++
Answer2: In multi-threaded environement they can be expensive
Answer3: objects with shared ownership can live longer than scoped object
Answer4: The execution is more unpredictable, which is an issue if delete is non trivial and unlock resource
Answer5: If a single (last) node keeps a large data structure alive, the cascade of destructor calls triggered by its deletion can cause a significant ‘‘garbage collection delay.’’ That can be detrimental to real-time response.

Instruction: sp.use_count()
Page: 990
Language: C++
Chapter: 34
Section: 3.1
Returns: the value of the use count (0 if the contained pointer is nullptr)
Variables: shared_ptr<T> sp

Instruction: sp.unique()
Page: 990
Language: C++
Chapter: 34
Section: 3.1
Returns: whether the use count is 1
Variables: shared_ptr<T> sp

Instruction: sp.owner_before(pp)
Page: 990
Language: C++
Chapter: 34
Section: 3.1
Returns: ?
Variables: shared_ptr<T> sp

Instruction: allocate_shared(
  a,
  args
)
Page: 992
Language: C++
Chapter: 34
Section: 3.1
Returns: a shared_pointer, with alolcator a, generated with T{args}
Variables: shared_ptr<T> sp

Typ: shared_ptr<T>
Meaning: A pointer, with a count of how many people care about this pointer and how many require no destruction.
When 0 people require no destruction, the object pointed to is deleted. When the last reference to this pointer is done, the pointer itself is deleted
Page: 993
Language: C++
Chapter: 34
Section: 3.1
Implementation: {
  int use_count = weak_use_count = 0;
  D deleter;
  T* ptr;
}

Name: An allocator
Definition: An object to provide a source of memory for a given type and a place to return that memory to once it is no
longer needed.
Page: 995
Chapter: 34
Section: 4
Notation: C++

Typ: allocator<T>
Function: a.allocate(n, hint=nullptr) return a pointer with enough space for n objects of type T. Hint is implementation dependant
Page: 995
Function2: a.deallocate(p, n) release space for n objects of type T pointed to by p
Chapter: 34
Extends: C++
Section: 4
Intuition: An object to provide a source of memory for a given type and a place to return that memory to once it is no longer needed.

Name: The default allocator
Definition: The allocator using new and delete
Page: 997
Chapter: 34
Section: 4.1
Context: C++

Question: How can GC leak?
Answer: an object may acquire resource and not release them at right time
Page: 1002
Chapter: 34
Section: 5
Context: C++
Answer2: A hash table may keep containing object that become useful. GC does not detect when the key is lost
Answer3: leak may take days instead of hours to crash the system, making them harder to detect
Answer4: In threads infinite by accident, resource may never be released (e.g. waiting for input that never arrives)

Question: How can GC allow a program to run
Answer: If it extends the lifetime before crash to more than a day and the program restart daily, the program is usable
Page: 1002
Chapter: 34
Section: 5
Context: C++
Answer2: some GC can be instrumented to find the sources of leaks

Name: Application Binary Interface
Page: 1003
Chapter: 34
Section: 5
Context: C++
Abbreviation: ABI

Name: A safely derived pointer
Definition: (roughly) ‘‘a pointer to something allocated by new or to a subobject thereof.’
Page: 1003
Chapter: 34
Section: 5
Context: C++

Question: Why does GC limit the use of pointer
Answer: A pointer to an array must be kept as is. You can't add to it, as the array will be seem to be dangling, even if you planned to subtract to it later to get the initial pointer back
Page: 1003
Chapter: 34
Section: 5
Context: C++
Answer2: pointer can't be written and read to external resource
Answer3: pointer can't be cast to int (which is a bad idea anyway)
Answer4: Pointer can't be xored (for the trick to compress double-linked list)

Name: A traceable pointer
Definition: A pointer that can be recognized as such even if it is not a pointer type
Page: 1004
Chapter: 34
Section: 5
Context: C++
Example: A pointer saved in long or char[4]
Counterexample: a pointer which is xored with another one (as in doubly linked list)

Instruction: declare_reachable(ptr)
Page: 1004
Effect: value pointed by ptr must not be collected by a GC
Language: C++
Chapter: 34
Section: 5

Instruction: undeclare_reachable(ptr)
Page: 1004
Effect: cancel declare_reachable(ptr)
Language: C++
Chapter: 34
Section: 5
Returns: ptr

Instruction: undeclare_no_pointers(
  ptr,
  n
)
Page: 1004
Effect: cancel undeclare_reachable(ptr, n)
Language: C++
Chapter: 34
Section: 5

Instruction: declare_no_pointers(
  ptr,
  n
)
Page: 1004
Effect: the values at ptr[0: n) contains no pointer
Language: C++
Chapter: 34
Section: 5
Example: Applied on some media data,
  it avoids the GB to scan mb or gb

Name: A conservative garbage collector
Definition: assumes that every word may contain a pointer
Page: 1004
Chapter: 34
Section: 5
Context: C++
Definition2: do not move objects around in memory
Conjdef: A garbage collector which

Name: Relaxed pointer safety and declaration
Definition: Safely-derived and not safely-derived pointers are treated equivalently (as in C and
C++98).
Page: 1004
Chapter: 34
Section: 5
Context: C++
Definition2: Collect every object that does not have a safely derived or traceable pointer to it.

Name: preferred pointer safety and declaration
Definition: same as relatex pointer safety and declaration
Page: 1004
Chapter: 34
Section: 5
Context: C++
Definition2: garbage collector may be running as a leak detector and/or a
detector of dereferences of ‘‘bad pointers.’’
Conjdef: both

Name: strict pointer safety and declaration
Definition: Safely-derived and not safely-derived pointers may be treated differently; that is, a
garbage collector may be running and will ignore pointers that’s not safely derived.
Page: 1004
Chapter: 34
Section: 5
Context: C++

Name: raw memory
Definition: Memory with random data
Page: 1004
Chapter: 34
Section: 5
Context: C++
Name2: Uninitialized memory

Question: When does it make sens to deal with unitialized memory
Answer: writing memory
allocators
Page: 1004
Chapter: 34
Section: 5
Context: C++
Answer2: implementing containers
Answer3: dealing directly with hardware

Instruction: get_temproary_buffer<T>(n)
Page: 1005
Language: C++
Chapter: 34
Section: 6.1
Returns: pair with:
* a pointer to some non-initialized memory with enough space for n or more objects of type T
* number of objects that can be stored
Intuition: to save temporary values for computation that is destroyed very quickly. For low level library functions

Instruction: return_temproary_buffer<T>(ptr)
Page: 1005
Language: C++
Chapter: 34
Section: 6.1
Returns: Free the spaces to which ptr points to. Do not destroy its content

Typ: raw_storage_iterator<Out, T>
Meaning: Iterator allowing to iterate over non initialized data
Page: 1005
Language: C++
Chapter: 34
Section: 6.2