-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scope_Stack_Life.cpp
69 lines (57 loc) · 2 KB
/
Scope_Stack_Life.cpp
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
//Whenever we enter a scope in C++ we push a stack frame on
//We regard this as a book pile where we can pull the top most-one and take out the one below
//We cannot yank any book from the stack
//We have to remember as soon as we remove the book from the stack it expires immediately
//any variable/any object we have within that book is gone
#include<iostream>
#include<string>
class Characteristic{
public:
Characteristic(){
std::cout<<"Created 2526 56837 6342"<<std::endl;
}
~Characteristic(){
std::cout<<"Destroyed 968 6342 56837!"<<std::endl;
}
};
class PointersWithAScope
{
private:
Characteristic* my_Pointer;
public:
PointersWithAScope(Characteristic* pointer) : my_Pointer(pointer)
{
}
~PointersWithAScope()
{
delete my_Pointer;
}
};
//Creating a pointer array
int* CreateMyArray()
{
int array[50];//Create space for me in memory of 200 bytes for me to store 50 integers
//This is declared on the stack
//return *array;//Return a pointer of the stack memory address
//The above code will get destroyed if we go out of scope because memory is allocated on the stack
//To avoid this we change it to this to allocate memory on the heap instead
//for it to stay around
int* myLastingArray = new int[50];
return myLastingArray;
//Never create a stack based variable and try to return a pointer that is not good coding practice
//An alternative is a smart pointer which is a scoped pointer. This is a wrapper class over a pointer
//which when created allocates memory on the heap and upon destruction deletes the pointer
}
int main()
{
{
Characteristic lovesC_And_TS;//Stack-based
Characteristic* lovesTSwift = new Characteristic();//Heap-based
PointersWithAScope element = new Characteristic;
}
//(STACK BASED VA) Now if we enter the debug mode and I reach line 59 my object
//gets destroyed because I went out of scope
//
//Line 60: However if I do a heap allocation things are different it never gets freed
//only if our application is closed thanks to our OS
}