Found in Example
Dynamically Allocated Heap
Statically Allocated Stack int x = 10;

Variable Scope

Pointers

int x;
int* p;

x = 7;
p = Null;
p = &x;

cout << *p << endl;
cout << p << endl;

1000025557.jpg


Dynamic Memory Allocation

malloc from C becomes new in C++

int* p = new int;

free from C becomes delete in C++

delete p;

Destructors

Updating our student class from last time.

class student{
	private:
		string name;
		int* grade;
	public:
		student();
		student(int);
}
student:student(){
	name = "";
	grade = NULL;
}

student::student(int n){
	grade = new int[n];
}
int main(){
	student x(10);
	return 0;
}

1000025558.jpg

Question: we just returned and exited the program, but our memory on the heap is still out there. How can we solve this?