1/5
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Primary reason:
If we want to allocate memory in a function and have it STAY ALIVE even AFTER that function ends
Secondary reason:
If we don't know how much memory we'll need until run-time
How do you allocate and free memory?
int *p = new int; // one int
int *arr = new int[n]; // array
delete p; // free one
delete[] arr; // free array
Where in the memory?
Lives in the Heap
New operator:
Allocates memory from the heap
double *dptr = new double;
int *scores = new int[n];
Delete:
Returns memory to heap
delete dptr;
delete [] scores;