-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
79 lines (66 loc) · 1.92 KB
/
simulator.cpp
File metadata and controls
79 lines (66 loc) · 1.92 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
#include <iostream>
using namespace std;
void staticAllocation() {
int arr[5] = {1, 2, 3, 4, 5};
cout << "\n--- Static Array ---\n";
for (int i = 0; i < 5; i++) {
cout << "[" << arr[i] << "] @ " << &arr[i] << endl;
}
}
void dynamicAllocation() {
int size;
cout << "\nEnter size of dynamic array: ";
cin >> size;
int* arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = (i + 1) * 10;
}
cout << "\n--- Dynamic Array ---\n";
for (int i = 0; i < size; i++) {
cout << "[" << arr[i] << "] @ " << &arr[i] << endl;
}
char resizeChoice;
cout << "\nDo you want to resize the array? (y/n): ";
cin >> resizeChoice;
if (resizeChoice == 'y' || resizeChoice == 'Y') {
int newSize;
cout << "Resize array to size: ";
cin >> newSize;
int* newArr = new int[newSize];
for (int i = 0; i < newSize && i < size; i++) {
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
cout << "\n--- Resized Array ---\n";
for (int i = 0; i < newSize; i++) {
cout << "[" << arr[i] << "] @ " << &arr[i] << endl;
}
}
delete[] arr;
}
int main() {
int choice;
do {
cout << "\n=== Memory Manager Simulator ===\n";
cout << "1. Static Allocation\n";
cout << "2. Dynamic Allocation\n";
cout << "3. Exit\n";
cout << "Choice: ";
cin >> choice;
switch (choice) {
case 1:
staticAllocation();
break;
case 2:
dynamicAllocation();
break;
case 3:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 3);
return 0;
}