-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_6.html
More file actions
89 lines (76 loc) · 3.18 KB
/
Copy pathCpp1_6.html
File metadata and controls
89 lines (76 loc) · 3.18 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
<article>
<h1>C++ I: Page 6 - Memory: Stack vs Heap</h1>
<section>
<h2>The Architecture of Memory</h2>
<p>Understanding where your data lives is what separates professional C++ developers from everyone else. C++ gives you explicit control over memory through two main mechanisms: the <strong>Stack</strong> and the <strong>Heap</strong>.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">
/*
* File: memory.cpp
* Demonstration of Stack vs Heap allocation and RAII concepts
*/
#include <iostream>
#include <memory> // For smart pointers
using namespace std;
// Stack allocation (Automatic)
void stackDemo() {
int x = 10;
cout << "Stack variable x: " << x << endl;
}
// Heap allocation (Manual)
void heapDemo() {
// Allocation on the heap using 'new'
int* y = new int(20);
cout << "Heap variable *y: " << *y << endl;
// Manual deallocation required
delete y;
}
// Modern C++ Memory Management
void smartPointerDemo() {
// std::unique_ptr manages heap memory automatically
unique_ptr<int> z = make_unique<int>(30);
cout << "Smart pointer *z: " << *z << endl;
// No 'delete' required!
}
int main() {
cout << "--- Stack Demo ---" << endl;
stackDemo();
cout << "--- Heap Demo ---" << endl;
heapDemo();
cout << "--- Smart Pointer Demo ---" << endl;
smartPointerDemo();
// Heap array
int* arr = new int[5]{1, 2, 3, 4, 5};
delete[] arr; // Crucial: Prevent memory leaks
return 0;
}
/*
* Memory Management Nuances:
* 1. Stack: Fast, automatic scope management (LIFO order).
* 2. Heap: Slower, manual lifetime management (new/delete).
* 3. Memory Leaks: Forgetting 'delete' on heap memory causes leaks,
* leading to increased memory usage over time.
* 4. Dangling Pointers: Accessing memory after 'delete' is undefined behavior,
* frequently causing crashes.
* 5. Modern C++: Smart pointers (unique_ptr, shared_ptr) are the standard
* solution to manual memory management problems.
* 6. Always use 'delete[]' for arrays allocated with 'new[]'.
* 7. Memory alignment and padding can affect the actual size of structs in memory.
*/
</code></pre>
<h3>Stack vs Heap Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_6');
out.innerHTML = 'Stack: x = 10;<br>Heap: int* y = new int(20);<br>';
setTimeout(() => out.innerHTML += '>> Stack freed automatically.<br>>> Heap needs delete!', 800);
">Run Simulation</button>
<div id="sim-output-cpp1_6" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_5.html" style="text-decoration: none; color: #6c757d;">← Previous: Loops: For & While</a>
<a href="#" data-file="Cpp1_7.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Pointers & References →</a>
</div>
</footer>
</article>