banner
Lowerce

Lowerce

github
twitter

C++ doubts and difficulties

From UP: HexUp
"Explaining one of the most important features of modern C++ in 30 minutes: Smart Pointers"

Type Declarations#

Follow the "right-left" rule
Start from the variable name and look to the right, then look to the left after finishing
Do not ignore parentheses, read layer by layer
The outer layer after jumping out represents the type of the inner element or the pointer pointing to the inner layer
The order of reading represents that the latter is the type of the former element
Declarations like const also follow the "right-left" rule

Pointer Size#

32-bit architecture and 64-bit architecture
The smallest unit of memory is 1 byte = 8 bits
Pointer size for 32-bit programs is 4 bytes
Pointer size for 64-bit programs is 8 bytes
The pointer size is essentially determined by the compiler

Lambda Expressions#

Also known as closures
Structure:
[captured variables](parameter list) -> return type {function body}
Variables are captured by value if no & is added, captured by reference if & is added
[&] captures all external variables by reference
[=] captures all external variables by value
The above two can be combined, [&,=N] captures all external variables by reference except for variable N which is captured by value

Raw Pointers and Dynamic Memory Allocation#

new is used to dynamically allocate memory
delete is used to manually release dynamically allocated memory, otherwise this memory will not be freed. If the space allocated by new is an array, delete[] should be used
Smart pointers are used to avoid manual memory management that may cause memory leaks

shared_ptr#

Requires #include <memory>, belongs to the std namespace
Initialization:
shared_ptr<int> p {make_shared<int>(100)};
shared_ptr<int> p {new int(100)};
shared_ptr<int> p = make_shared<int>(100);
The above methods are completely equivalent
p.use_count() returns the number of pointers pointing to the same object
p.reset() resets the pointer, no longer pointing to the original object; it can also be set with a parameter to point to a new object
When the shared_ptr pointing to an object becomes 0, the object will be automatically destroyed
p.get() gets the raw pointer pointing to the current object
There are also some special parameter configurations for initialization as follows:
shared_ptr<FILE> sfp {fp, close_file}; The second parameter is a custom destruction function
shared_ptr<Bar> b(f, &(f->b)); The second parameter is used to access the member variables of a class

unique_ptr#

Basic library, initialization method is the same as shared_ptr
Exclusive ownership of the object, automatically releases the bound object when destroyed
p.release() returns the raw pointer of the object and sets p to nullptr at the same time
The ownership cannot be copied but can be transferred
unique_ptr<int> p2 {p1.release()}
unique_ptr<int> p2 {move(p1)}
Transfers the ownership of p1 to p2, the above two forms are equivalent
unique_ptr<int, decltype(&my_dealloc)> cup {my_alloc(100), my_dealloc} Custom destruction functions are more complex and need to be declared inside <>
When passing between functions, you can directly pass the value using *p, or pass the raw pointer using p.get(), or set the function's parameter as unique_ptr<int>&, or use move(p) to change the ownership of the object (in this case, the function's parameter is unique_ptr<int>)
There are no additional restrictions on the return value of the function, you can directly initialize unique_ptr<int> with a function return value of type unique_ptr<int>, which essentially calls move

weak_ptr#

Can solve the problem of circular dependencies
weak_ptr depends on the existence of shared_ptr and needs to be initialized with shared_ptr
weak_ptr only has the observation right of the object, not the management right of the object
wp.lock() returns a shared_ptr, if the resource pointed to by the weak_ptr has been released, it returns nullptr

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.