📖 Scenario: Imagine you are creating a program that stores the names of students in a class. Sometimes the class size changes, and you don't know the number of students before running the program.
🎯 Goal: You will learn why dynamic memory is needed by creating a program that stores student names using dynamic memory allocation in C++.
📋 What You'll Learn
Create a pointer to store student names dynamically
Use new to allocate memory for student names
Use a variable to store the number of students
Use a loop to input student names
Print all student names stored in dynamic memory
💡 Why This Matters
🌍 Real World
Dynamic memory is used in programs where the amount of data changes, like storing user input, files, or network data.
💼 Career
Understanding dynamic memory is important for software developers working with C++ to manage resources efficiently and avoid crashes.
Progress0 / 4 steps
1
Create a variable for the number of students
Create an int variable called numStudents and set it to 3.
C++
Hint
Use int numStudents = 3; to store the number of students.
2
Create a dynamic array for student names
Create a pointer to string called students and use new to allocate an array of numStudents strings.
C++
Hint
Use std::string* students = new std::string[numStudents]; to allocate dynamic memory.
3
Input student names using a loop
Use a for loop with variable i from 0 to numStudents - 1 to input student names into students[i].
C++
Hint
Use for (int i = 0; i < numStudents; i++) and std::getline(std::cin, students[i]); to read names.
4
Print student names and free memory
Use a for loop with variable i from 0 to numStudents - 1 to print each student name from students[i]. Then use delete[] students; to free the dynamic memory.
C++
Hint
Use a loop to print names and delete[] students; to free memory.
Practice
(1/5)
1. Why do we need dynamic memory in C++ programs?
easy
A. To write code without any variables
B. To allocate memory during program execution when size is unknown
C. To avoid using any memory at all
D. To make the program run faster by using fixed memory
Solution
Step 1: Understand memory allocation timing
Static memory is fixed before running, but dynamic memory is allocated while the program runs.
Step 2: Recognize the need for unknown sizes
When the amount of data is not known before running, dynamic memory allows flexible allocation.
Final Answer:
To allocate memory during program execution when size is unknown -> Option B
Quick Check:
Dynamic memory = flexible size allocation [OK]
Hint: Dynamic memory = memory decided during program run [OK]
Common Mistakes:
Thinking dynamic memory is for speed only
Confusing dynamic with static memory
Believing dynamic memory avoids variables
2. Which of the following is the correct syntax to allocate an integer dynamically in C++?
easy
A. int *p = new int;
B. int p = new int;
C. int *p = int new;
D. new int = int *p;
Solution
Step 1: Recall dynamic allocation syntax
In C++, dynamic memory for an int is allocated using 'new int' and assigned to an int pointer.
Step 2: Check pointer declaration
The pointer must be declared as 'int *p' to hold the address of the allocated int.
Final Answer:
int *p = new int; -> Option A
Quick Check:
Pointer = new type; correct syntax [OK]
Hint: Use 'int *p = new int;' for dynamic int allocation [OK]
Common Mistakes:
Missing pointer * in declaration
Wrong order of new and type
Assigning new int to non-pointer variable
3. What will be the output of this C++ code snippet?