#include using namespace std; #include "Student.h" int main() { // automatic memory allocation // (allocated on the runtime stack) int x = 5; double y; while ( x > 0 ) { // static memory allocation // (variables are not eliminated when they go out scope) // (and they instead retain their values) static bool first_time = true; if ( first_time ) { cout << "first time through the loop, x is " << x << '\n'; first_time = false; } x--; } // dynamic memory allocation (allocated on the runtime heap) // (use the "new" keyword) int * p = new int; *p = 400; cout << "p points to an int with value " << *p << '\n'; int * q = new int; *q = *p; *p = 500; cout << "p points to " << *p << " and q points to " << *q << '\n'; delete p; // relinquish that memory delete q; // new scope.... { double * p = new double; *p = 35.1; double * q = p; cout << *p << " and " << *q << '\n'; p = new double; *p = 27.1; cout << *p << " and " << *q << '\n'; *q = 12.5; cout << *p << " and " << *q << '\n'; delete p; delete q; cout << *p << " and " << *q << '\n'; double * r = new double; *r = 48.6; cout << *p << " and " << *q << '\n'; } // also declare arrays dynamically int n = 500; double * a = new double[n]; // (1) Allocate memory // do something with a ... use *a or a[i] (2) use memory a[10] = 3.14; *(a+10) = 3.14; delete [] a; // (3) Deallocate memory // Student * students = new Student[n]; // requires default constructor // do stuff with those 500 objects.... // delete [] students; return 0; }