Dynamic Memory Allocation and Smart Pointers

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:21 PM on 8/11/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

31 Terms

1
New cards

Where are fixed-size arrays stored?

Stack memory

2
New cards

How is stack memory managed?

Automatically managed by the program

3
New cards

What is a limitation of fixed-size arrays?

Size must be known at compile time and cannot change

4
New cards

Why is dynamic memory allocation needed?

When the required size is only known at runtime.

5
New cards

What is the heap?

A region of memory managed by the programmer

6
New cards

What operator allocates memory on the heap?

new

int* p = new int;

7
New cards

How do you allocate memory for a single variable?

int* p = new int;

8
New cards

How do you allocate memory for an array?

int* arr = new int[n];

9
New cards

How do you release dynamically allocated memory?

delete p;
delete[] arr;

10
New cards

Why is delete[] used instead of delete for arrays?

To release memory allocated for an array

11
New cards

What does the constructor do in the IntArray example?

#include <iostream>

class IntArray
{
private:
    int *data_;
    int size_;

public:
    IntArray(int s)
    {
        size_ = s;
        data_ = new int[size_];

        for (int i = 0; i < size_; ++i)
        {
            data_[i] = 0;
        }
    }

Allocates memory and initializes all elements to 0

12
New cards

What does the copy constructor do?

Creates a new array and copies every element

13
New cards

What does the assignment operator do?

Deletes old memory, allocates new memory, and copies the data

14
New cards

Why does the assignment operator check this != &other?

To avoid self-assignment

15
New cards

What does the destructor do?

Releases dynamically allocated memory using delete[].

16
New cards

Why do a and b remain independent after copying?

Each object has its own dynamically allocated array

17
New cards

What is a memory leak?

Dynamically allocated memory is not released after it is no longer needed

18
New cards

What causes a memory leak?

Using new without calling delete

19
New cards

What can happen if memory leaks accumulate?

The program can slow down or crash

20
New cards

How can memory leaks be avoided?

Ensure every new has a corresponding delete

21
New cards

Why can exceptions cause memory leaks?

delete may never be reached

22
New cards

How does modern C++ help avoid memory leaks?

By using smart pointers

23
New cards

What is a smart pointer?

An object that behaves like a pointer but automatically manages dynamic memory

24
New cards

What is one benefit and one cost of smart pointers?

Prevent memory leaks; slightly more overhead

25
New cards

When should std::unique_ptr be used?

When an object should have exactly one owner

std::unique_ptr<int[]> p(new int[size]);

26
New cards

What happens when a std::unique_ptr goes out of scope?

The object is automatically destroyed

27
New cards

Can a std::unique_ptr be copied?

No, ownership must be transferred using move()

std::unique_ptr<int[]> p1(new int[5]);
std::unique_ptr<int[]> p2;

p2 = std::move(p1);

28
New cards

How is ownership transferred with std::unique_ptr?

p2 = std::move(p1);

29
New cards

When should std::shared_ptr be used?

When multiple parts of a program must own the same object

std::shared_ptr<int[]> p(new int[size]);

30
New cards

What is a circular reference?

Two or more objects reference each other, preventing the reference count from reaching zero

31
New cards

When should std::weak_ptr be used?

To observe a shared object without owning it and avoid circular references

std::shared_ptr<int[]> p(new int[size]);
std::weak_ptr<int[]> w = p;