📖 Scenario: Imagine you are managing a small library system where you need to store the number of books available in different sections. You want to learn how to create space in memory to store this data and then clean up the space when you are done.
🎯 Goal: You will write a simple C++ program that allocates memory dynamically for storing the number of books in three sections, assigns values to them, and then frees the memory.
📋 What You'll Learn
Use new to allocate memory for an array of integers
Assign values to the allocated memory
Use delete[] to free the allocated memory
Print the values stored in the allocated memory
💡 Why This Matters
🌍 Real World
Dynamic memory allocation is used in programs where the amount of data is not known before running the program, like managing lists of items or user inputs.
💼 Career
Understanding memory allocation and deallocation is essential for software developers to write efficient and safe C++ programs, especially in systems programming and game development.
Progress0 / 4 steps
1
Create a pointer and allocate memory
Create an integer pointer called books and allocate memory for 3 integers using new.
C++
Hint
Use int* books = new int[3]; to allocate memory for 3 integers.
2
Assign values to the allocated memory
Assign the values 10, 20, and 30 to the first, second, and third elements of the books array respectively.
C++
Hint
Use books[0] = 10; and similarly for the other elements.
3
Print the values stored in the allocated memory
Use a for loop with variable i from 0 to 2 to print each value in the books array separated by spaces.
C++
Hint
Use a for loop and std::cout to print each element.
4
Deallocate the memory
Use delete[] books; to free the memory allocated for the books array. Then print a newline character.
C++
Hint
Use delete[] books; to free the memory and then print a newline.
Practice
(1/5)
1. What does the new operator do in C++?
easy
A. Allocates memory on the heap for a variable or object
B. Deletes a variable from memory
C. Allocates memory on the stack
D. Copies data from one variable to another
Solution
Step 1: Understand the role of new
The new operator allocates memory dynamically on the heap during program execution.
Step 2: Differentiate from other memory actions
It does not delete memory or allocate on the stack; it only reserves space on the heap.
Final Answer:
Allocates memory on the heap for a variable or object -> Option A
Quick Check:
new = allocate heap memory [OK]
Hint: Remember: new = allocate memory on heap [OK]
Common Mistakes:
Confusing new with delete
Thinking new allocates on stack
Assuming new copies data
2. Which of the following is the correct syntax to deallocate memory allocated with new for a single integer variable?
easy
A. delete[] ptr;
B. free(ptr);
C. delete ptr;
D. ptr = nullptr;
Solution
Step 1: Identify correct deallocation for single object
Memory allocated with new for a single variable must be freed with delete, not delete[].
Step 2: Exclude incorrect options
free() is for C-style malloc, and setting pointer to nullptr does not free memory.
Final Answer:
delete ptr; -> Option C
Quick Check:
Single object deallocation = delete [OK]
Hint: Use delete for single object, delete[] for arrays [OK]
Step 1: Analyze memory allocation and initialization
The pointer ptr points to a new integer initialized with 10.
Step 2: Understand output before deletion
Dereferencing *ptr prints 10 before memory is deleted.
Final Answer:
10 -> Option B
Quick Check:
Dereference new int(10) = 10 [OK]
Hint: Dereference pointer before delete to see value [OK]
Common Mistakes:
Assuming output is 0 or garbage
Thinking delete affects output before cout
Confusing syntax causing compile error
4. Identify the error in the following code snippet:
int* arr = new int[5]; delete arr;
medium
A. Pointer arr is not initialized
B. Memory is not allocated properly
C. No error, code is correct
D. Should use delete[] instead of delete
Solution
Step 1: Check allocation type
Memory allocated with new[] must be deallocated with delete[].
Step 2: Identify mismatch in deallocation
Using delete instead of delete[] causes undefined behavior.
Final Answer:
Should use delete[] instead of delete -> Option D
Quick Check:
Array deallocation = delete[] [OK]
Hint: Match new[] with delete[] always [OK]
Common Mistakes:
Using delete for arrays
Ignoring pointer initialization
Assuming no error in mismatched delete
5. You want to allocate memory for an array of 10 integers and initialize all elements to zero. Which code snippet correctly does this and safely deallocates the memory?
hard
A. int* arr = new int[10](); delete[] arr;
B. int* arr = new int[10]; delete arr;
C. int* arr = (int*)malloc(10 * sizeof(int)); delete[] arr;
D. int* arr = new int(10); delete[] arr;
Solution
Step 1: Allocate and initialize array to zero
Using new int[10]() allocates an array of 10 integers and initializes all to zero.
Step 2: Properly deallocate array memory
Memory allocated with new[] must be freed with delete[] to avoid leaks or undefined behavior.
Final Answer:
int* arr = new int[10](); delete[] arr; -> Option A
Quick Check:
new[] with () + delete[] = safe zero-init array [OK]
Hint: Use new int[size]() and delete[] for arrays [OK]
Common Mistakes:
Using delete instead of delete[] for arrays
Mixing malloc/free with new/delete
Allocating single int instead of array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.