The new operator helps you create variables or objects in memory while your program is running. It lets you ask for space to store data when you need it.
When you want to create a variable but don't know how many you need until the program runs.
When you want to keep data even after a function ends.
When you want to create objects that live until you decide to remove them.
When you want to manage memory yourself for better control.
When you want to create arrays with sizes decided during the program.
Syntax
C++
pointer_variable = new data_type;
pointer_variable = new data_type[size];
The new operator returns a pointer to the memory it allocates.
Remember to use delete or delete[] to free memory later.
Examples
Creates a single integer in memory and p points to it.
C++
int* p = newint;
Creates an array of 5 doubles in memory and arr points to the first element.
C++
double* arr = newdouble[5];
Creates a single char initialized with 'A'.
C++
char* c = newchar('A');
Sample Program
This program creates one integer and an array of three integers using new. It stores values, prints them, and then frees the memory with delete and delete[].
C++
#include <iostream>
int main() {
int* num = newint; // create one int
*num = 42; // store 42 in itint* arr = newint[3]; // create array of 3 ints
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
std::cout << "Single number: " << *num << "\n";
std::cout << "Array elements: ";
for (int i = 0; i < 3; i++) {
std::cout << arr[i] << " ";
}
std::cout << "\n";
delete num; // free single int
delete[] arr; // free arrayreturn0;
}
OutputSuccess
Important Notes
Always pair new with delete to avoid memory leaks.
Use delete[] for arrays created with new[].
Access the allocated memory through the pointer returned by new.
Summary
new creates variables or arrays in memory during program run.
It returns a pointer to the allocated memory.
Always free memory with delete or delete[] when done.
Practice
(1/5)
1. What does the new operator do in C++?
easy
A. Allocates memory dynamically and returns a pointer to it
B. Deletes a variable from memory
C. Declares a normal variable on the stack
D. Copies data from one variable to another
Solution
Step 1: Understand the purpose of new
The new operator is used to allocate memory on the heap during program execution.
Step 2: Identify what new returns
It returns a pointer to the allocated memory so the program can use it.
Final Answer:
Allocates memory dynamically and returns a pointer to it -> Option A
Quick Check:
new = allocate memory [OK]
Hint: Remember: new creates memory and gives a pointer [OK]
Common Mistakes:
Confusing new with delete
Thinking new declares stack variables
Assuming new copies data
2. Which of the following is the correct syntax to allocate an integer using new?
easy
A. int p = new int;
B. int p = new int();
C. int *p = new int[];
D. int *p = new int;
Solution
Step 1: Check pointer declaration
Using new returns a pointer, so the variable must be a pointer type like int *.
Step 2: Verify syntax correctness
int *p = new int; correctly allocates an int and assigns its address to p.
Final Answer:
int *p = new int; -> Option D
Quick Check:
Pointer = new type [OK]
Hint: new returns pointer, so use pointer variable [OK]
Common Mistakes:
Declaring variable without pointer
Using parentheses incorrectly
Assigning new to non-pointer variable
3. What is the output of this code?
int *arr = new int[3]{10, 20, 30};
std::cout << arr[1] << std::endl;
delete[] arr;
medium
A. 20
B. 10
C. 30
D. Runtime error
Solution
Step 1: Understand array allocation with new
The code creates an array of 3 integers with values 10, 20, 30.
Step 2: Access the second element
Array indexing starts at 0, so arr[1] is the second element, which is 20.
Final Answer:
20 -> Option A
Quick Check:
arr[1] = 20 [OK]
Hint: Remember array index starts at 0 [OK]
Common Mistakes:
Confusing index 1 with index 0
Forgetting to use delete[] for arrays
Expecting runtime error without cause
4. Identify the error in this code snippet:
int *p = new int;
*p = 5;
delete p;
delete p;
medium
A. Incorrect use of new operator
B. Missing initialization of pointer
C. Double deletion of the same pointer
D. No error, code is correct
Solution
Step 1: Analyze pointer allocation and deletion
Pointer p is allocated memory and deleted once correctly.
Step 2: Check for repeated delete
Deleting p a second time causes undefined behavior (double deletion error).
Final Answer:
Double deletion of the same pointer -> Option C
Quick Check:
delete twice = error [OK]
Hint: Delete pointer only once to avoid errors [OK]
Common Mistakes:
Deleting pointer twice
Not deleting allocated memory
Confusing delete and delete[]
5. You want to create a dynamic 2D array of integers with 3 rows and 2 columns using new. Which code correctly allocates and deallocates this array?
hard
A. int *arr = new int[3*2];
// use arr
delete arr;
B. int **arr = new int*[3];
for(int i=0; i<3; i++) arr[i] = new int[2];
// use arr
for(int i=0; i<3; i++) delete[] arr[i];
delete[] arr;
C. int **arr = new int[3][2];
// use arr
delete[] arr;
D. int arr[3][2];
// use arr
// no delete needed
Solution
Step 1: Allocate 2D array dynamically
int **arr = new int*[3];
for(int i=0; i<3; i++) arr[i] = new int[2];
// use arr
for(int i=0; i<3; i++) delete[] arr[i];
delete[] arr; correctly creates an array of pointers for rows, then allocates each row as an array of ints.
Step 2: Properly deallocate memory
Each row is deleted with delete[], then the array of pointers is deleted with delete[].
Final Answer:
Uses an array of pointers, allocating each row separately -> Option B
Quick Check:
Nested new and delete[] for 2D arrays [OK]
Hint: Use nested new and delete[] for 2D arrays [OK]
Common Mistakes:
Using delete instead of delete[] for arrays
Not deleting all allocated rows
Trying to allocate 2D array with single new incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.