C++ new Keyword
The new keyword in C++ is used to dynamically allocate memory on the heap for variables, arrays, or objects at runtime. It returns a pointer to the allocated memory and ensures that the memory remains allocated until explicitly deallocated using the delete keyword.
Dynamic memory allocation with new is useful when the size or number of elements to be allocated is not known at compile time.
Syntax
</>
Copy
data_type* pointer_name = new data_type;
data_type* pointer_name = new data_type[size]; // For arrays
- data_type
- The type of the variable or object to allocate memory for.
- pointer_name
- The name of the pointer that will store the address of the allocated memory.
- size
- The number of elements to allocate when creating arrays dynamically.
Examples
Example 1: Allocating Memory for a Single Variable
In this example, you will learn how to use new to allocate memory for a single integer.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int* ptr = new int; // Allocate memory for an integer
*ptr = 42; // Assign a value to the allocated memory
cout << "Value: " << *ptr << endl;
delete ptr; // Free the allocated memory
return 0;
}
Output:
Value: 42
Explanation:
- The
newkeyword allocates memory for a single integer and returns its address, which is stored inptr. - The value
42is assigned to the allocated memory using*ptr. - The memory is released using the
deletekeyword to prevent memory leaks.
Example 2: Allocating Memory for an Array
In this example, you will learn how to use new to allocate memory for an array of integers.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int size = 5;
int* arr = new int[size]; // Allocate memory for an array of integers
for (int i = 0; i < size; ++i) {
arr[i] = i + 1; // Assign values to the array
}
cout << "Array elements: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
delete[] arr; // Free the allocated memory
return 0;
}
Output:
Array elements: 1 2 3 4 5
Explanation:
- The
newkeyword allocates memory for an array of size5and returns a pointer to the first element. - Values are assigned to the array using a loop.
- The
delete[]keyword is used to release the memory allocated for the array.
Example 3: Allocating Memory for a Custom Object
In this example, you will learn how to use new to allocate memory for an object of a user-defined class.
</>
Copy
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width;
public:
Rectangle(int l, int w) : length(l), width(w) {}
int area() {
return length * width;
}
};
int main() {
Rectangle* rect = new Rectangle(5, 3); // Allocate memory for a Rectangle object
cout << "Area: " << rect->area() << endl;
delete rect; // Free the allocated memory
return 0;
}
Output:
Area: 15
Explanation:
- The
newkeyword allocates memory for aRectangleobject and calls its constructor. - The pointer
rectis used to access the object's methods using the->operator. - The memory allocated for the object is released using
delete.
Key Points about new Keyword
- The
newkeyword dynamically allocates memory on the heap at runtime. - It returns a pointer to the allocated memory, which can be used to access or manipulate the data.
- Always pair
newwithdelete(ordelete[]for arrays) to avoid memory leaks. - Use
newfor scenarios where memory requirements are determined at runtime.
