Dynamic Memory Allocation and Classes Flashcards

1
Q

What is static memory allocation?

A

Memory allocation at compile time, where the size and types for the storage to be allocated are known in advance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is dynamic memory allocation?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you declare a dynamic memory allocation?

A

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 &laquo_space;arrPtr[i] &laquo_space;” “;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you deallocate dynamically allocated memory once it is no longer needed?

A

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 &laquo_space;arrPtr[i] &laquo_space;” “;
}

// Deallocating
delete numPtr; // Pointer to a dynamic variable
delete[] arrPtr; // Pointer to a dynamic array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are three common things that you can dynamically allocate?

A

variables, objects, and arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When you use the delete operator on a pointer that is pointing to memory allocated previously by the new operator, what happens?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What should you do with a pointer that was pointing to memory that is now deallocated?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the arrow operator (->) used for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When creating a pointer as class member data, how should you set it up?

A
  1. Declare the pointer (uninitialized) in the header file of the class.
  2. Initialize the pointer (to dynamic space or otherwise) in the constructor(s) – ALWAYS!
  3. If there are pointers to dynamically allocated memory, use delete to deallocate in the destructor (right now so you don’t forget it.)
  4. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Should you put a dynamic array in the member data section of a class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where in a class should you be using the new operator to dynamically allocate memory?

A

In the member functions (constructors and possibly other functions depending on the class’ needs.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is creating specific member functions to handle allocation and deallocation a good idea?

A

Yes.
You can call the memory management functions any time you want to manually create or delete dynamic memory, which keeps things more simple.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly