CS Week 6- Pointer and Memory Management Flashcards
new
allocates memory for the given type and returns a pointer to the allocated memory
look at notes and lab notes for the coding stuff
:)
different ways to use new
- just one object
int* point = new int;
2.one object with a specific value
int* point = new int(10);
- a class
int* point = new Class; - a class with its specific values
int* point = new Class(77,88); - an array of objects
int* point = new int[10]; - an array of classes
int* point = new Class[10];
member access operator
->
allows the pointer to access member function
new only one class
not an array of classes
delete
deallocates (or frees) a block of memory that was allocated with the new operator
After delete, no attempt to dereference because the value does not exist anymore
when to use delete and delete[]
delete when there is only one thing/object allocated
delete[] when there is an array of objects
code
region where the program instructions are stored
Static memory-
region where global variables (variables declared outside any function) as well as static local variables (variables declared inside functions starting with “static”) are allocated.
-Static variables are allocated once and stay in the same memory location for the duration of a program’s execution
the stack
region where a function’s local variables are allocated during a function call
-A function calls adds them (to the top of the stack) and a return removes them
-Think of the picture tyler drew with the stack and how function call are on top of main and return a value down and cannot be access again until another call
-Also called automatic memory - this memory is automatically allocated and unallocated
the heap
region where the “new” operator allocates memory and where “delete” operator deallocates memory
Also called free store
memory leak
occurs when a program that allocates memory loses the ability to access the allocated memory, typically due to failure to properly destroy/free dynamically allocated memory
free stuff that is no longer used with delete or delete[]
free stuff as private pointers (sub-object) with delete or delete[] in deconstructors
garbage collection
automatic process of fencing and freeing unreachable allocated memory locations
inline member function
a member function’s definition may appear within the class definition
Use to yield more compact code, keeping longer definitions outside the class
except with declaring variables before use
Constructor initializer list
an alternative approach for initializing data member in constructor, coming after a colon and consisting of a comma-separated list of variableName(initValue) items
-Have them in the order they were declared in class definition
-Avoid creating and then modifying, all one step
Standard template library (STL)
defines classes for common Abstract Data Types(ADT)