0% found this document useful (0 votes)
2 views3 pages

Practical 3

The document presents a C++ program that demonstrates dynamic memory allocation and deallocation using 'new' and 'delete'. It allocates memory for a single integer and an array, initializes the array with user input, and ensures proper memory management by deallocating the memory and nullifying pointers. The program emphasizes safety by preventing memory leaks and dangling pointers.

Uploaded by

hardikdeotale7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Practical 3

The document presents a C++ program that demonstrates dynamic memory allocation and deallocation using 'new' and 'delete'. It allocates memory for a single integer and an array, initializes the array with user input, and ensures proper memory management by deallocating the memory and nullifying pointers. The program emphasizes safety by preventing memory leaks and dangling pointers.

Uploaded by

hardikdeotale7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ program that demonstrates dynamic memory allocation and

deallocation:

#include <iostream>

using namespace std;

int main() {

// Dynamically allocate memory for an integer

int* num = new int; // Allocate memory

*num = 100; // Assign a value

cout << "Dynamically allocated integer: " << *num << endl;

// Deallocate the memory for the integer

delete num;

num = nullptr; // Nullify the pointer for safety

// Dynamically allocate memory for an array

int n;

cout << "Enter the size of the array: ";

cin >> n;

int* arr = new int[n]; // Allocate memory for an array

// Initialize and display the array

cout << "Enter " << n << " elements for the array:" << endl;

for (int i = 0; i < n; i++) {

cin >> arr[i];

cout << "The elements of the array are: ";

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

cout << endl;


// Deallocate the memory for the array

delete[] arr;

arr = nullptr; // Nullify the pointer for safety

cout << "Memory deallocated successfully!" << endl;

return 0;

Explanation:

1. Dynamic Memory Allocation:

o new is used to allocate memory dynamically on the heap.

o int* num = new int; allocates memory for a single integer.

o int* arr = new int[n]; allocates memory for an array of n


integers.

2. Memory Access and Initialization:

o For a single integer, use the dereference operator (*) to assign


and retrieve the value.

o For an array, use subscript notation (arr[i]) to access


elements.

3. Dynamic Memory Deallocation:

o Use delete to deallocate memory for a single object.

o Use delete[] to deallocate memory for an array.

o Assign nullptr to the pointer after deletion to prevent dangling


pointer issues.

4. Safety:

o The program ensures proper memory deallocation, avoiding


memory leaks.

Output Example:

Dynamically allocated integer: 100

Enter the size of the array: 5

Enter 5 elements for the array:

12345
The elements of the array are: 1 2 3 4 5

Memory deallocated successfully!

You might also like