-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
57 lines (50 loc) · 2.06 KB
/
main.cpp
File metadata and controls
57 lines (50 loc) · 2.06 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
#include "includes/bst/BST.h"
struct item
{
item(int a) : _a(a) {}
int _a = 0;
friend ostream& operator<<(ostream& os, item const& i)
{
os << "A Printed Item.";
return os;
}
};
int main()
{
// create pointers
Ptr<int> p1;
Ptr<int> array;
Ptr<item> itemPtr;
// allocate memory
p1._new(); // allocates one spot
array._new(5); // allocates five spots
itemPtr._new(); // allocates one spot
cout << "Allocated Pointers." << endl;
cout << "P1:\t\tAddress: " << p1 << "\tItem: " << *p1 << endl;
cout << "Array:\t\tAddress: " << array << "\tItem: " << *array << endl;
cout << "ItemPtr:\tAddress: " << itemPtr << "\tItem: " << *itemPtr << endl << endl;
*p1 = -12367890; // assign p1's value as -1234567890
*array = 123; // assign first value of array
*(array + 1) = 1; // assign index 1 of 'array' to 1
*(++array) = 1; // assign index 1 of 'array' to 1 (again)
*(array++) = 1; // assign index 1 of 'array' to 1 (again)
array = array - 2; // return array to original index
*(array + 2) = 2; // assign index 2 of 'array' to 2
*(array + 3) = 3; // assign index 3 of 'array' to 3
*itemPtr = item(5); // assign itemPtr value to a
cout << "Assigned Pointers." << endl;
cout << "P1:\t\tAddress: " << p1 << "\tItem: " << *p1 << endl;
cout << "Array:\n";
for (int i = 0; i < 5; i++)
cout << "Index: " << i << "\t\tAddress: " << array + i << "\tItem: " << *(array + i) << endl;
cout << "ItemPtr:\tAddress: " << itemPtr << "\tItem: " << *itemPtr << endl;
cout << "Item's I using arrow operator: " << itemPtr->_a << endl << endl;
_memory.print_allocated();
// deallocate all
bool brackets = true;
p1._delete(); // deallocates space for p1
array._delete(brackets); // deallocates space for ENTIRE array
itemPtr._delete(); // deallocates space for itemPtr
_memory.print_allocated();
return 0;
}