Back To Basics Lifetime Management
^~~~~~The compiler will generate Special Member Functions (SMFs) for you 1. Defining some SMFs may delete or disable some others 2. Special Member Functions T(T const&) T() T(T&&) operator=(T const&) Gadget* gadget; Widget() : age(42), gadget(new Gadget()) {} ~Widget() { delete gadget; } }; std::cout << widget.name << "\n"; std::cout << widget.age << "\n"; std::cout << Gadget* gadget; Widget() : age(42), gadget(new Gadget()) {} ~Widget() { delete gadget; } }; std::cout << widget.name << "\n"; std::cout << widget.age << "\n"; std::cout <<0 码力 | 66 页 | 8.43 MB | 5 月前3Better Code: Exploring Validity
struct P { P() { x = new int32_t(); try { y = new int32_t(); } catch(...) { delete x; throw; } } ~P() { delete x; delete y; } private: int32_t * x; int32_t * y; };© 2023 Adobe. All Rights Reserved. struct P { P() { x = new int32_t(); try { y = new int32_t(); } catch(...) { delete x; throw; } } ~P() { delete x; delete y; } private: int32_t * x; int32_t * y; };© 2023 Adobe. All Rights Reserved. struct P { P() { x = new int32_t(); try { y = new int32_t(); } catch(...) { delete x; throw; } } ~P() { delete x; delete y; } private: int32_t * x; int32_t * y; };© 2023 Adobe. All Rights Reserved.0 码力 | 117 页 | 6.03 MB | 5 月前3Object Lifetime: From Start to Finish
Deallocation when static program begins program ends thread_local thread begins thread ends Dynamic new delete Automatic enclosing block begins* enclosing block ends* 12 Storage Duration13 { }Allocation Deallocation when static program begins program ends thread_local thread begins thread ends Dynamic new delete Automatic enclosing block begins* enclosing block ends* 14 Storage DurationNon-vacous initialization { std::cout << "~Foo()" << std::endl; } }; int main() { Foo* a = nullptr; { a = new Foo(); } delete a; } 1 2 3 4 5 6 7 8 9 10 11 https://godbolt.org/z/hY7hr5Efr 32 Constructors & Destructors (2)struct0 码力 | 214 页 | 9.34 MB | 5 月前3Implementing Particle Filters with Ranges
13 sample_view(const sample_view&) = delete; 14 sample_view(sample_view&&) = default; 15 sample_view& operator=(const sample_view&) = delete; 16 sample_view& operator=(sample_view&&) 13 sample_view(const sample_view&) = delete; 14 sample_view(sample_view&&) = default; 15 sample_view& operator=(const sample_view&) = delete; 16 sample_view& operator=(sample_view&&) base ; 23 sample_view(const sample_view&) = delete; sample_view(sample_view&&) = default; sample_view& operator=(const sample_view&) = delete; sample_view& operator=(sample_view&&) = default;0 码力 | 83 页 | 4.70 MB | 5 月前3Hidden Overhead of a Function API
.LBB1_2 b operator delete(void*) .LBB1_2: ret mov rax, QWORD PTR [rdi] mov QWORD PTR [rdi], 0 test rax, rax je .L3 mov esi, 4 mov rdi, rax jmp operator delete(void*) .L3: ret mov PTR [rcx], 0 test rax, rax je SHORT $LN34@output_ptr mov edx, 4 mov rcx, rax jmp operator delete(void*) $LN34@output_ptr: ret 0 O U T P U T 25Returning std::unique_ptr #includestd::unique_ptr x29, sp add x8, x29, #24 bl value_ptr() ldr x0, [x29, #24] ldr w19, [x0] bl operator delete(void*) mov w0, w19 ldr x19, [sp, #16] ldp x29, x30, [sp], #32 ret push rbx sub rsp, 16 0 码力 | 158 页 | 2.46 MB | 5 月前3The Roles of Symmetry And Orthogonality In Design
oneself” { ... Bar* b = new Bar(); ConsumeBar(*b); } void ConsumeBar(Bar& b) { ProcessBar(b); delete b; //...consume } Can implement designs where state escapes compiler-defined control flow governed DoThing1(*b); } ConsumeBar(*b); } } void ConsumeBar(Bar& b) { //...do stuff with b ProcessBar(b); delete b; //...consume } void DoThing0(Bar& b) { //...do stuff with b } void DoThing1(Bar& b) { // DoThing1(*b); } ConsumeBar(*b); } } void ConsumeBar(Bar& b) { //...do stuff with b ProcessBar(b); delete b; //...consume } void DoThing0(Bar& b) { //...do stuff with b } void DoThing1(Bar& b) { //0 码力 | 151 页 | 3.20 MB | 5 月前3Back to Basics: Move Semantics
Move Constructor struct S { double* data; // Invariant: data != nullptr S( S&& other ) noexcept = delete; }; Assumes that data == nullptr is a valid state If not, then a move constructor is not possibleMove Assignment Operator struct S { double* data; S& operator=( S&& other ) noexcept { delete[] data; data = std::exchange(other.data, nullptr); return *this; } }; How to write one101 David Move Assignment Operator struct S { double* data; S& operator=( S&& other ) noexcept { delete[] data; data = std::exchange(other.data, nullptr); return *this; } }; Not quite done… We’ll come 0 码力 | 142 页 | 1.02 MB | 5 月前3Delivering safe C++
programmer’s intent, such as using < where a <= or a > was intended. • Resource leaks: failing to delete resources (e.g., memory, file handles, and locks) potentially leading to the program grinding to A “delete function” reverses that • Later (1983) • “new function” -> constructor • “delete function” -> destructor • Because constructors and destructors were/are not just for the new and delete operators vector• string • ifstream • unique_ptr • shared_ptr • RAII • “No naked new” • “No naked delete” Stroustrup - C++ safety -CppCon - October 2023 40 A resource leak is potentially damagingPointer 0 码力 | 74 页 | 2.72 MB | 5 月前3C++26 Preview
expected 1, got {}", sizeof(S))) 9.1relevant blog post P2573 = delete("should have a reason"); void newapi(); void oldapi() = delete("oldapi() is outdated and been removed - newapi()."); templatedelete("Using rvalue may result in dangling reference"); 1 2 3 4 5 6 7 8 9 https://quuxplusone.github.io/blog/2021/10/17/equals-delete-means/ 10relevant blog post P2573 = delete("should have have a reason"); void newapi(); void oldapi() = delete("oldapi() is outdated and been removed - newapi()."); template struct A {/* ... */}; template A factory(const T&) {/* 0 码力 | 118 页 | 2.02 MB | 5 月前3CppCon 2021: Persistent Data Structures
PersStatus 6: Maybe \\Default value 7: InProgress 8: Persisted 9: enum OpType 10: Insert 11: Delete 12: Find 13: struct Operation 14: OpType type 15: int key 16: struct Desc 17: int size 18: int Descriptor t1 t2 t3 Thread 1 Thread 2 Thread 3 Insert(3) Insert(1) Insert(4) Insert(2) Delete(3) Delete(4) Replaced by t3 Access conflict with t2 opid: 1 txdesc:t1 opid: 1 txdesc:t2 opid: 0 txdesc:t1 Type: Insert, key: 4 Type: Insert, key: 2 Status: Active PStatus: Maybe Size: 2 Type: Delete, key: 3 Type: Delete, key: 4 Status: Active PStatus: Maybe Size: 2 Crash in the middle of t2 and t3 execution0 码力 | 56 页 | 1.90 MB | 5 月前3
共 163 条
- 1
- 2
- 3
- 4
- 5
- 6
- 17