Dynamic Memory Allocation and Classes Flashcards
What is static memory allocation?
Memory allocation at compile time, where the size and types for the storage to be allocated are known in advance.
What is dynamic memory allocation?
Memory allocation at run time, where the size of the storage is not known in advance but can be set while the program is running.
How do you declare a dynamic memory allocation?
Initialize a pointer and assign it dynamic memory with the new keyword followed by a type (which should be compatible with the pointer).
// Declaring a pointer to a new integer with the initial value 10
int * numPtr = new int(10);
// Declaring a pointer to a new array of doubles
double * arrPtr = new double[*numPtr];
// Initializing the array and outputting values
for (int i = 0; i < *numPtr; i++)
{
arrPtr[i] = i+1;
std::cout «_space;arrPtr[i] «_space;” “;
}
How do you deallocate dynamically allocated memory once it is no longer needed?
The delete operator. See bottom of example below for deallocation.
// Declaring a pointer to a new integer with the initial value 10
int * numPtr = new int(10);
// Declaring a pointer to a new array of doubles
double * arrPtr = new double[*numPtr];
// Initializing the array and outputting values
for (int i = 0; i < *numPtr; i++)
{
arrPtr[i] = i+1;
std::cout «_space;arrPtr[i] «_space;” “;
}
// Deallocating
delete numPtr; // Pointer to a dynamic variable
delete[] arrPtr; // Pointer to a dynamic array
What are three common things that you can dynamically allocate?
variables, objects, and arrays
When you use the delete operator on a pointer that is pointing to memory allocated previously by the new operator, what happens?
The dynamic memory the pointer is pointing to will be deallocated, but the pointer will still remain.
You -cannot- delete the pointer itself. It is ALWAYS pointing to something, even if you don’t know what/where it’s pointing to!
What should you do with a pointer that was pointing to memory that is now deallocated?
Point it to the null pointer.
intPtr = nullptr;
This allows for control over pointers because any “empty” pointer can be detected by simply checking to see if it’s pointing to nullptr.
What is the arrow operator (->) used for?
It functions similarly to the . operator in that in can call members of a class (functions, data), but it’s used specifically for pointers.
// Arrow operator calls a member function on the object contained in the pointer
myPtr->Show();
// Pointers can use the . operator, but it’s more confusing due to operator precedence requiring the use of ()
(*myPtr).Show();
// This starts to get confusing if you’re using pointers to pointers, so the arrow operator is best practice.
When creating a pointer as class member data, how should you set it up?
- Declare the pointer (uninitialized) in the header file of the class.
- Initialize the pointer (to dynamic space or otherwise) in the constructor(s) – ALWAYS!
- If there are pointers to dynamically allocated memory, use delete to deallocate in the destructor (right now so you don’t forget it.)
- If a pointer does not immediately need to be pointing at dynamic memory, best practice is to point it at nullptr in the constructor until such time as you need it.
Should you put a dynamic array in the member data section of a class?
No. Only the POINTER you intend to use for that dynamic array should be there. Assign the new dynamic array in the constructor for the class’ objects.
Where in a class should you be using the new operator to dynamically allocate memory?
In the member functions (constructors and possibly other functions depending on the class’ needs.)
Is creating specific member functions to handle allocation and deallocation a good idea?
Yes.
You can call the memory management functions any time you want to manually create or delete dynamic memory, which keeps things more simple.