-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointers.cpp
More file actions
88 lines (75 loc) · 2.33 KB
/
Copy pathPointers.cpp
File metadata and controls
88 lines (75 loc) · 2.33 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
//Implement reference counting with shared pointer
#include<iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
using namespace std;
template <typename T>
class SharedPtr {
T* ptr;
int* refCount;
public:
SharedPtr(T*p = nullptr) : ptr(p), refCount(new int(1)) {}
SharedPtr(const SharedPtr& other) {
ptr = other.ptr;
refCount = other.refCount;
++(*refCount);
}
SharedPtr& operator =(const SharedPtr& other) {
if(this!=&other) {
if(--(*refCount) == 0) {
delete ptr;
delete refCount;
}
ptr = other.ptr;
refCount = other.refCount;
++(*refCount);
}
}
~SharedPtr() {
if (--(*refCount) == 0) {
delete ptr;
delete refCount;
}
}
T& operator*() {return *ptr;}
T* operator->(){return ptr;}
};
volatile bool data_ready_flag = false;
//interrupt occurs
void interrupt_handler() {
data_ready_flag = true;
}
int add(int a, int b) { return a + b; }
int main() {
//unique pointer - sole ownership
std::unique_ptr<int> ptr = std::make_unique<int>(10);
//shared pointer-> reference counting
std::shared_ptr<int> sp1 = std::make_shared<int>(5);
std::shared_ptr<int> sp2 = sp1; // count++
//weak pointer
std::weak_ptr<int> wp = sp1;
volatile uint32_t* hardware_status_register = (volatile uint32_t*)0x12345000;
//Tells compiler not to optimize reads/writes (used in hardware register reads, ISR communication).
//Marking the pointer as volatile ensures the program always reads the current status from memory.
//for real-time ISR communication with main loop
interrupt_handler();
while (true) {
if (data_ready_flag) {
// Process the data, always checking the current value in memory
//process_data();
data_ready_flag = false;
}
}
int x = 5;
int *p = &x;
int **pp = &p;
//Function pointers
int (*fptr)(int, int) = &add;
//Dangling pointer
int* p;
{
int x = 5;
p = &x;
} // p is now dangling
}