Memory Allocation (3.5-3.6)

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

16 Terms

1
New cards
What is the heap in C++?
The heap is an area of memory set aside for storing larger values created during program execution, and is used to allocate memory for data types that occupy more memory, like arrays.
2
New cards

How do you allocate memory on the heap in C++?

Memory is allocated on the heap using the new keyword. For example: new int; allocates memory for a single integer on the heap (with nothing assigned to it).

NOTE: This code doesn’t make sense on it’s own. It must have a pointer like: int* ptr = new int(10);

3
New cards
What is a pointer in C++?
A pointer is a variable that stores the memory address of another variable. It is declared by writing the type being pointed to, followed by the symbol *.
4
New cards

How do you declare an integer pointer in C++?

To declare an integer pointer: int * p_i; creates a pointer to an integer (but with nothing actually assigned).

5
New cards

How do you assign a pointer to newly allocated memory?

You assign a pointer to newly allocated memory like this: p_i = new int; assigns memory for an integer to the pointer p_i.

To put simply, this assigns memory for a new integer variable and then sets the pointer to that location. Full code:

<p>You assign a pointer to newly allocated memory like this: p_i = new int; assigns memory for an integer to the pointer p_i.<br><br>To put simply, this assigns memory for a new integer variable and then sets the pointer to that location. Full code:</p>
6
New cards
How do you dereference a pointer in C++?
You dereference a pointer using the unary * operator. For example, *p_j = 10; accesses the value stored at the memory address pointed to by p_j.
7
New cards
What does the dereference operator do?
The dereference operator (*) allows you to access or modify the value stored at the memory address that a pointer is pointing to.
8
New cards
How do you print the memory address stored in a pointer?
You can print the memory address stored in a pointer using cout << p_j << endl;.
9
New cards
How do you print the value stored at the memory address of a pointer?
You print the value stored at the memory address of a pointer using cout << *p_j << endl;.
10
New cards
What is the shorthand for calling a function via a pointer?
The shorthand for calling a function via a pointer is the -> operator, which combines dereference (*) and function accessor (.) into one step.
11
New cards
How do you dereference and call a method on a pointer in C++?
You can dereference and call a method like this: (*p_s).substr(2,4); or using the shorthand p_s->substr(2,4);.
12
New cards

How do you take the address of a non-pointer variable?

You take the address of a non-pointer variable using the unary & operator. For example, int * p_i = &i; takes the address of i and assigns it to p_i.

Reminder: int * p_i is creating a new pointer, not dereferencing it.

13
New cards
What is the delete operator used for in C++?
The delete operator is used to free memory that was allocated using new, informing the operating system that the memory is no longer needed.
14
New cards
Why is it important to free memory in C++?
It is important to free memory to avoid exhausting heap space, which can lead to out-of-memory exceptions and crashes.
15
New cards

How do you free a block of memory allocated with new?

You free a block of memory using the delete operator. For example: delete p_array; frees the memory pointed to by p_array (notice it is a pointer to a variable).

16
New cards
What is a potential issue with not freeing memory in a loop?
If memory is not freed in a loop, each iteration allocates more memory, which can lead to heap exhaustion and crash the program.