University of Lethbridge - CPSC
Resizing the Arrays
Assignment 1 - Movie1 - SM 25What does this function do?
This function doubles the size of the titles[] and years[] arrays when they run out of space. At the start of the program, the arrays are small (capacity 2), but as the user keeps entering movies, we eventually run out of room. This function:
Allocates new, larger arrays
Copies all the old data into the new ones
Deletes the old arrays
Updates the original pointers to point to the new arrays
Doubles the value of
capacity
Why do we need this?
C++ arrays have a fixed size once they are created. If we want to add more data than the array can hold, we must create a new, bigger array and move everything over manually.
This is what resize() does.
Without this function, the program would crash or lose data once the user enters more than two movies.
Explaining the Parameters
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 27 more lessons in University of Lethbridge - CPSC.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.