-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_and_heap.cpp
More file actions
133 lines (101 loc) · 3 KB
/
stack_and_heap.cpp
File metadata and controls
133 lines (101 loc) · 3 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
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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <array>
#include <memory> //for delete operator overload
using namespace std;
class Student{
public: string name;
void print(){ cout << name << endl;
}
Student(string name) : name(name){ }
};
//overide new operator to keep track of memory
void* operator new(size_t size){
cout << "Allocating " << size << "bytes\n";
return malloc(size);
}
void operator delete(void* memory, size_t size){
cout << "freeing " << size << endl;
free(memory);
}
int main(){
int x = 4; //on the stack, in functions local variables are automaticly cleaned
int *p = new int(5); // now using memory on the heap
//*p = 5;
//data on the heap needs to be deleted.
delete p;
//if you change p wihout freeing the 5 it will cause a memory leak. You can no longer clear that space.
//example
// int *p = new int(5);
// p = new int(10);
// delete p;
double *array = new double[4]; //a block
//can use like an array
array[0] = 5;
array[1] = 6;
array[2] = 7;
array[3] = 8;
delete array;
//Object created on the heap
Student *student = new Student("joseph"); // the keyword "new" calls the constructor
(*student).print();
(*student).name = "John";
(*student).print();
student->name = "Mary";
student->print();
delete student; // the "delete" keyword calls the destructor
//old C -- malloc() , calloc() , free
//when a block of memory is too large
/*
try{
double *big_array = new double [9999999999999999999999];
} catch (bad_alloc& exp){
cout << "bad_alloc_caught: " << exp.what() << endl;
}
double *big_again = new(nothrow) double [9999999999999999999999];
if(big_again == NULL)
cout << "failed to allocate again." << endl;
*/
double *mydouble = new double(12.2);
cout << *mydouble << endl;
double *specific = new(mydouble) double(20.5); // re-using existing allocated memory
cout << *mydouble << endl;
}
//------------------------------------------THIS IS A MEMORY LEAK----------------------------
int memoryLeak(){
int *p = new int(5);
//if you change p wihout freeing the 5 it will cause a memory leak. You can no longer clear that space.
//NOTICE: p is using the keyword "new" while not being initialized.
p = new int(10);
//now we can'd delete '5'
delete p;
}
//Does this fix it?
int fixLeak(){
int *p = new int(5);
// assign another pointer to '5'.
int *q = p;
//NOTICE: p is using the keyword "new" while not being initialized.
p = new int(10); //now we can'd delete '5'
delete p;
//then delete that.
delete q;
}
//STACK
//------ know how much data we are going to be working with
//
// memory
// Variable Address Value
//
// x 0x0001 4
// p 0x0002 0x9901
//
//
//
// HEAP
//-------to allocate memory dynamiccly
//
// 0x9901 5
// 0x9902
// 0x9903