-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
472 lines (388 loc) · 29.5 KB
/
Copy pathapi.cpp
File metadata and controls
472 lines (388 loc) · 29.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
clang++ -std=c++20 -O0 -Wall -Wextra api.cpp -o api
./api
clang++ -std=c++20 -O1 -fsanitize=address api.cpp -o api_asan
./api_asan
*/
#include <iostream>
#include <utility>
#include <vector>
struct Obj {
int id;
Obj(int i) : id(i) { std::cout << "CTOR " << id << "\n"; }
Obj(const Obj &o) : id(o.id) { std::cout << "COPY " << id << "\n"; }
Obj(Obj &&o) noexcept : id(o.id) {
std::cout << "MOVE " << id << "\n";
o.id = -1; // mark moved-from (debug only)
}
~Obj() { std::cout << "DTOR " << id << "\n"; }
};
// -------- APIs demonstrating ownership --------
void read_only(const Obj &o) { std::cout << "READ " << o.id << "\n"; }
void mutate(Obj &o) { o.id += 10; }
void take_by_value(Obj o) { std::cout << "TAKE VALUE " << o.id << "\n"; }
void sink(Obj &&o) { std::cout << "SINK " << o.id << "\n"; }
// -------- Main --------
int main() {
std::cout << "\n=== reserve prevents reallocation ===\n";
std::vector<Obj> v;
v.reserve(3); // critical
v.emplace_back(1); // in-place
v.emplace_back(2);
v.emplace_back(3);
std::cout << "\n=== push_back vs emplace_back ===\n";
v.push_back(Obj(4)); // temp + MOVE
v.emplace_back(5); // direct construct
std::cout << "\n=== iterator invalidation demo ===\n";
auto it = v.begin(); // points to v[0]
std::cout << "Iterator points to: " << it->id << "\n";
// Uncommenting this may invalidate it if capacity exceeded
// v.emplace_back(6);
std::cout << "Iterator still points to: " << it->id << "\n";
std::cout << "\n=== API ownership patterns ===\n";
Obj a(100);
read_only(a); // const T&
mutate(a); // T&
take_by_value(a); // COPY
take_by_value(Obj(200)); // MOVE
sink(std::move(a)); // explicit consume
std::cout << "\n=== vector pass by value (ownership) ===\n";
std::vector<Obj> v2;
v2.reserve(2);
v2.emplace_back(10);
v2.emplace_back(20);
auto process = [](std::vector<Obj> vec) { // cppcheck-suppress passedByValue
std::cout << "PROCESS size=" << vec.size() << "\n";
};
process(std::move(v2)); // O(1) buffer move
std::cout << "\n=== End of main ===\n";
}
/*
═══════════════════════════════════════════════════════════════════════════
PART 1: v.reserve(3) - PRE-ALLOCATION
═══════════════════════════════════════════════════════════════════════════
BEFORE reserve: AFTER v.reserve(3):
STACK: STACK:
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ vector v: │ │ vector v: │
│ size: 0 │ │ size: 0 │
│ capacity: 0 │ │ capacity: 3 │
│ buffer: nullptr │ │ buffer → HEAP ───┐ │
└──────────────────────────────┘ └─────────────────────┼────────┘
│
HEAP: HEAP: │
(nothing allocated) ┌────────────────────┼──────────┐
│ ◄─────────────────┘ │
│ [ ??? ][ ??? ][ ??? ] │
│ └─ 3 slots (uninitialized) │
└───────────────────────────────┘
Memory allocated but NO objects constructed yet!
═══════════════════════════════════════════════════════════════════════════
PART 2: v.emplace_back(1), v.emplace_back(2), v.emplace_back(3)
═══════════════════════════════════════════════════════════════════════════
After emplace_back(1): After emplace_back(2):
STACK: STACK:
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ vector v: │ │ vector v: │
│ size: 1 │ │ size: 2 │
│ capacity: 3 │ │ capacity: 3 │
│ buffer → HEAP ───┐ │ │ buffer → HEAP ────┐ │
└─────────────────────┼────────┘ └──────────────────────┼───────┘
│ │
HEAP: │ HEAP: │
┌─────────────────────┼─────────┐ ┌─────────────────────┼─────────┐
│ ◄──────────────────┘ │ │ ◄──────────────────┘ │
│ [Obj:1][ ??? ][ ??? ] │ │ [Obj:1][Obj:2][ ??? ] │
│ ↑ CTOR 1 │ │ ↑ CTOR 2 │
└───────────────────────────────┘ └───────────────────────────────┘
After emplace_back(3):
STACK:
┌──────────────────────────────┐
│ vector v: │
│ size: 3 │
│ capacity: 3 (FULL!) │
│ buffer → HEAP ───┐ │
└─────────────────────┼────────┘
│
HEAP: │
┌─────────────────────┼─────────┐
│ ◄──────────────────┘ │
│ [Obj:1][Obj:2][Obj:3] │
│ ↑ CTOR 3 │
└───────────────────────────────┘
Output: "CTOR 1" "CTOR 2" "CTOR 3"
↑ Each constructed DIRECTLY in the vector's buffer
═══════════════════════════════════════════════════════════════════════════
PART 3: v.push_back(Obj(4)) - CAPACITY EXCEEDED, REALLOCATION HAPPENS
═══════════════════════════════════════════════════════════════════════════
Step 1: Create temporary Obj(4)
┌──────────────────────────────┐
│ Temporary on stack: │
│ Obj temp: │ Output: "CTOR 4"
│ id = 4 │
└──────────────────────────────┘
Step 2: Capacity exceeded! Reallocate
OLD HEAP (0x1000): NEW HEAP (0x2000):
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ [Obj:1][Obj:2][Obj:3] │ │ [ ??? ][ ??? ][ ??? ] │
│ (will be moved from) │ │ [ ??? ][ ??? ][ ??? ] │
└──────────────────────────────┘ │ └─ Allocated (capacity ~6) │
└──────────────────────────────┘
Step 3: Move existing objects to new buffer
OLD HEAP: NEW HEAP:
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ [moved][moved][moved] │ │ [Obj:1][Obj:2][Obj:3] │
│ ↓ ↓ ↓ │ │ ↑ ↑ ↑ │
│ (to be destroyed) │ │ MOVE 1, MOVE 2, MOVE 3 │
└──────────────────────────────┘ └──────────────────────────────┘
Output: "MOVE 1" "MOVE 2" "MOVE 3"
Step 4: Destroy old objects
OLD HEAP:
┌──────────────────────────────┐
│ [DTOR][DTOR][DTOR] │
│ └─ Old memory freed │
└──────────────────────────────┘
Output: "DTOR -1" "DTOR -1" "DTOR -1" (moved-from objects)
Step 5: Move temporary into new buffer
NEW HEAP:
┌───────────────────────────────┐
│ [Obj:1][Obj:2][Obj:3][Obj:4] │
│ ↑ │ Output: "MOVE 4"
│ (from temp)│
└───────────────────────────────┘
Step 6: Destroy temporary
Output: "DTOR -1" (temporary was moved-from)
FINAL STATE:
STACK:
┌──────────────────────────────┐
│ vector v: │
│ size: 4 │
│ capacity: 6 │
│ buffer → 0x2000 ───┐ │
└───────────────────────┼──────┘
│
HEAP (0x2000): │
┌───────────────────────┼──────┐
│ ◄────────────────────┘ │
│ [Obj:1][Obj:2][Obj:3][Obj:4]│
│ [ ??? ][ ??? ] │
└──────────────────────────────┘
═══════════════════════════════════════════════════════════════════════════
PART 4: v.emplace_back(5) - WITH AVAILABLE CAPACITY
═══════════════════════════════════════════════════════════════════════════
BEFORE: AFTER:
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ vector v: │ │ vector v: │
│ size: 4 │ │ size: 5 │
│ capacity: 6 │ │ capacity: 6 │
│ buffer → HEAP ───┐ │ │ buffer → HEAP ───┐ │
└─────────────────────┼────────┘ └─────────────────────┼────────┘
│ │
HEAP: │ HEAP: │
┌─────────────────────┼────────┐ ┌─────────────────────┼────────┐
│ ◄──────────────────┘ │ │ ◄──────────────────┘ │
│ [1][2][3][4][ ??? ][ ??? ] │ │ [1][2][3][4][5][ ??? ] │
│ │ │ ↑ CTOR 5 │
└──────────────────────────────┘ │ (constructed in-place!) │
└──────────────────────────────┘
No reallocation! No moves!
Output: "CTOR 5" only
═══════════════════════════════════════════════════════════════════════════
PART 5: ITERATOR INVALIDATION
═══════════════════════════════════════════════════════════════════════════
auto it = v.begin();
┌──────────────────────────────┐
│ Iterator it: │
│ ptr → 0x2000 ─────┐ │ Points to v[0]
└──────────────────────┼───────┘
│
HEAP (0x2000): │
┌──────────────────────┼───────┐
│ ◄────────────────────┘ │
│ [Obj:1][Obj:2][Obj:3]... │
│ ↑ │
│ it points here │
└──────────────────────────────┘
Output: "Iterator points to: 1"
IF v.emplace_back(6) were called AND caused reallocation:
┌──────────────────────────────┐
│ Iterator it: │
│ ptr → 0x2000 ──────X │ ⚠️ DANGLING!
└──────────────────────────────┘ Old buffer freed!
HEAP (0x2000 - FREED): HEAP (0x3000 - NEW):
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ ❌ DEALLOCATED MEMORY │ │ [1][2][3][4][5][6] │
│ ⚠️ Accessing 'it' = UB │ │ └─ Vector moved here │
└──────────────────────────────┘ └──────────────────────────────┘
But since capacity=6 and size=5, one more element fits without reallocation!
So the iterator remains valid.
═══════════════════════════════════════════════════════════════════════════
PART 6: API OWNERSHIP PATTERNS
═══════════════════════════════════════════════════════════════════════════
Obj a(100); Output: "CTOR 100"
┌──────────────────────────────┐
│ STACK: │
│ Obj a: │
│ id = 100 │
└──────────────────────────────┘
─────────────────────────────────────────────────────────────────────────
read_only(a); // const Obj& o
─────────────────────────────────────────────────────────────────────────
┌──────────────────────────────┐
│ main(): │
│ Obj a: id=100 ◄───┐ │ Output: "READ 100"
├──────────────────────┼───────┤
│ read_only(): │ │ ✅ No copy
│ const Obj& o ─────┘ │ ✅ Read-only access
│ (8 byte reference) │ ✅ Fast
└──────────────────────────────┘
─────────────────────────────────────────────────────────────────────────
mutate(a); // Obj& o
─────────────────────────────────────────────────────────────────────────
┌──────────────────────────────┐
│ main(): │
│ Obj a: id=100 ◄───┐ │ After: a.id = 110
├──────────────────────┼───────┤
│ mutate(): │ │ ✅ No copy
│ Obj& o ───────────┘ │ ✅ Can modify original
│ o.id += 10 │ ✅ Fast
└──────────────────────────────┘
─────────────────────────────────────────────────────────────────────────
take_by_value(a); // Obj o
─────────────────────────────────────────────────────────────────────────
┌──────────────────────────────┐
│ main(): │ Output: "COPY 110"
│ Obj a: id=110 │ "TAKE VALUE 110"
├──────────────────────────────┤ "DTOR 110"
│ take_by_value(): │
│ Obj o: id=110 │ ❌ Makes copy
│ (separate object) │ ❌ Slower
│ } → DTOR called │ ✅ Safe (can't modify original)
└──────────────────────────────┘
─────────────────────────────────────────────────────────────────────────
take_by_value(Obj(200)); // Temporary
─────────────────────────────────────────────────────────────────────────
Step 1: Create temporary
┌──────────────────────────────┐
│ Obj(200) temporary: │ Output: "CTOR 200"
│ id = 200 │
└──────────────────────────────┘
Step 2: Move to parameter
┌──────────────────────────────┐
│ take_by_value(): │ Output: "MOVE 200"
│ Obj o: id=200 │ "TAKE VALUE 200"
│ (moved from temp) │ ✅ Move (not copy)
└──────────────────────────────┘ "DTOR 200"
"DTOR -1" (temp)
─────────────────────────────────────────────────────────────────────────
sink(std::move(a)); // Obj&& o (rvalue reference)
─────────────────────────────────────────────────────────────────────────
BEFORE: AFTER:
┌──────────────────────────────┐ ┌──────────────────────────────┐
│ main(): │ │ main(): │
│ Obj a: id=110 │ │ Obj a: id=-1 │
│ ↓ move │ │ (moved-from state) │
│ (cast to rvalue) │ │ │
├──────────────────────────────┤ ├──────────────────────────────┤
│ sink(): │ │ sink(): │
│ Obj&& o → refers to a │ │ Obj&& o │
│ o.id is still 110 │ │ Output: "SINK 110" │
│ (reference, not moved!) │ │ │
└──────────────────────────────┘ └──────────────────────────────┘
⚠️ Important: Obj&& is just a reference to an rvalue!
The move constructor is NOT called.
The function can read/modify the object, but it's not "moved into" the
function.
═══════════════════════════════════════════════════════════════════════════
PART 7: process(std::move(v2)) - VECTOR MOVE
═══════════════════════════════════════════════════════════════════════════
BEFORE move:
STACK:
┌──────────────────────────────┐
│ main(): │
│ vector v2: │
│ size: 2 │
│ capacity: 2 │
│ buffer → 0x4000 ────┐ │
└──────────────────────────┼───┘
│
HEAP (0x4000): │
┌──────────────────────────┼───┐
│ ◄───────────────────────┘ │
│ [Obj:10][Obj:20] │
└──────────────────────────────┘
Output: "CTOR 10" "CTOR 20"
DURING process(std::move(v2)):
STACK:
┌──────────────────────────────┐
│ main(): │
│ vector v2: │
│ size: 0 ←────┐ │ v2 is now empty!
│ capacity: 0 │ │ (moved-from state)
│ buffer: nullptr │ │
├──────────────────────────┼───┤
│ process(): │ │
│ vector vec: │ │
│ size: 2 │ │ Output: "PROCESS size=2"
│ capacity: 2 │ │
│ buffer → 0x4000 ────┼───┤ Ownership transferred!
└──────────────────────────┼───┘
│
HEAP (0x4000): │
┌──────────────────────────┼────┐
│ ◄───────────────────────┘ │
│ [Obj:10][Obj:20] │
│ └─ SAME buffer (not copied!) │
└───────────────────────────────┘
╔═══════════════════════════════════════════════════════════════╗
║ VECTOR MOVE SEMANTICS ║
║ • Only the control block is copied (~24 bytes) ║
║ • The heap buffer pointer is transferred ║
║ • Source vector left empty (not destroyed) ║
║ • O(1) operation - no matter how large the vector! ║
║ • When vec goes out of scope, it frees the buffer ║
╚═══════════════════════════════════════════════════════════════╝
AFTER process() returns:
STACK:
┌──────────────────────────────┐
│ main(): │
│ vector v2: │
│ (empty) │ v2 still exists but empty
└──────────────────────────────┘
Output: "DTOR 10" "DTOR 20"
HEAP: (vec's destructor freed buffer)
(buffer 0x4000 freed)
═══════════════════════════════════════════════════════════════════════════
SUMMARY: API PATTERNS
═══════════════════════════════════════════════════════════════════════════
┌──────────────────┬─────────────────┬─────────────────┬──────────────┐
│ Pattern │ Syntax │ Copy/Move? │ Use When │
├──────────────────┼─────────────────┼─────────────────┼──────────────┤
│ Read-only │ const T& │ ❌ Neither │ Just reading │
│ (borrow) │ │ (reference) │ Don't modify │
│ │ │ │ │
│ Mutable borrow │ T& │ ❌ Neither │ Modify orig │
│ (in/out param) │ │ (reference) │ No ownership │
│ │ │ │ │
│ Take ownership │ T │ ✅ Copy/Move │ Need own copy│
│ (by value) │ │ (lvalue=copy) │ or consume │
│ │ │ (rvalue=move) │ │
│ │ │ │ │
│ Consume rvalue │ T&& │ ❌ Reference │ Forward/sink │
│ (rvalue ref) │ │ (but can move │ Perfect fwd │
│ │ │ from it) │ │
└──────────────────┴─────────────────┴─────────────────┴──────────────┘
PERFORMANCE COMPARISON:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Operation Cost
─────────────────────────────────────────────────────────────────
read_only(a) O(1) - just pointer
mutate(a) O(1) - just pointer
take_by_value(a) O(n) - full copy
take_by_value(Obj(200)) O(1) - move (cheap)
sink(std::move(a)) O(1) - just reference
process(std::move(v2)) O(1) - just pointer steal
Vector reallocation: O(n) - move all elements
• With reserve(): Avoided!
• Without reserve(): Happens multiple times
*/