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 | #include <iostream>
#include <vector>
#include <memory>
#include <random>
// Memory Manager for 3D grid using raw pointers
class MemoryManager {
public:
MemoryManager(int x, int y, int z) : x_(x), y_(y), z_(z) {
std::cout << "Allocating 3D grid of size " << x_ << " x " << y_ << " x " << z_ << std::endl;
grid_ = new double**[x_];
for (int i = 0; i < x_; ++i) {
grid_[i] = new double*[y_];
for (int j = 0; j < y_; ++j) {
grid_[i][j] = new double[z_];
}
}
}
~MemoryManager() {
std::cout << "Deallocating 3D grid" << std::endl;
for (int i = 0; i < x_; ++i) {
for (int j = 0; j < y_; ++j) {
delete[] grid_[i][j];
}
delete[] grid_[i];
}
delete[] grid_;
}
void setValue(int x, int y, int z, double value) {
if (x < x_ && y < y_ && z < z_) {
grid_[x][y][z] = value;
}
}
double getValue(int x, int y, int z) const {
if (x < x_ && y < y_ && z < z_) {
return grid_[x][y][z];
}
return 0.0; // Handle out of bounds
}
private:
int x_, y_, z_;
double*** grid_;
};
// Particle class for particle system simulation
class Particle {
public:
Particle(double x, double y, double z) : x_(x), y_(y), z_(z) {}
void move(double dx, double dy, double dz) {
x_ += dx; y_ += dy; z_ += dz;
}
void print() const {
std::cout << "Particle at (" << x_ << ", " << y_ << ", " << z_ << ")" << std::endl;
}
private:
double x_, y_, z_;
};
// Particle system using std::shared_ptr
class ParticleSystem {
public:
void addParticle(double x, double y, double z) {
particles_.push_back(std::make_shared<Particle>(x, y, z));
}
void moveParticles() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-0.1, 0.1);
for (auto& particle : particles_) {
particle->move(dis(gen), dis(gen), dis(gen));
}
}
void printParticles() const {
for (const auto& particle : particles_) {
particle->print();
}
}
private:
std::vector<std::shared_ptr<Particle>> particles_;
};
// Grid class for unique ownership demonstration
class Grid {
public:
Grid(int size) : size_(size) {
std::cout << "Creating Grid of size " << size_ << std::endl;
}
~Grid() {
std::cout << "Destroying Grid of size " << size_ << std::endl;
}
int getSize() const { return size_; }
private:
int size_;
};
// Simulation class using std::unique_ptr for Grid ownership
class Simulation {
public:
Simulation(int gridSize) : grid_(std::make_unique<Grid>(gridSize)) {}
void run() {
std::cout << "Running simulation with grid size " << grid_->getSize() << std::endl;
}
private:
std::unique_ptr<Grid> grid_;
};
int main() {
// Demonstrate MemoryManager with raw pointers
{
MemoryManager mm(4, 4, 4);
mm.setValue(2, 2, 2, 3.14);
std::cout << "Value at (2, 2, 2): " << mm.getValue(2, 2, 2) << std::endl;
}
std::cout << "\n";
// Demonstrate ParticleSystem with std::shared_ptr
{
ParticleSystem ps;
ps.addParticle(0, 0, 0);
ps.addParticle(1, 1, 1);
std::cout << "Initial particle positions:" << std::endl;
ps.printParticles();
ps.moveParticles();
std::cout << "Particle positions after movement:" << std::endl;
ps.printParticles();
}
std::cout << "\n";
// Demonstrate Simulation with std::unique_ptr
{
Simulation sim(10);
sim.run();
}
return 0;
}
|