0% found this document useful (0 votes)
3 views2 pages

Dynamic Memory Allocation With Pointer

The document provides a C++ code example demonstrating dynamic memory allocation for an array, allowing user input for its elements, displaying the array, and deallocating memory. Key functions include 'createDynamic' for memory allocation and input, and 'showArray' for displaying the contents. It emphasizes the importance of proper memory management to avoid leaks.

Uploaded by

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

Dynamic Memory Allocation With Pointer

The document provides a C++ code example demonstrating dynamic memory allocation for an array, allowing user input for its elements, displaying the array, and deallocating memory. Key functions include 'createDynamic' for memory allocation and input, and 'showArray' for displaying the contents. It emphasizes the importance of proper memory management to avoid leaks.

Uploaded by

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

LECTURE 23

Md. Minhaz Akram


EEE 1109

Dynamic Memory Allocation in C++


Write a C++ code that demonstrates how to dynamically allocate memory for an array, populate it with user-provided
values, display the array, and then deallocate the memory.

CODE:

#include<iostream>
using namespace std;

void createDynamic(int*& p, int& N)


{
cout<< "Number of elements in the array:";
cin >> N;
p = new int [N];
for(int i=0;i<N;i++)
{
cout<< "Enter " << i << " th element:";
cin >> p[i];
}
}

void showArray(int* p, int N)


{
for(int i=0;i<N;i++)
{
cout << p[i] << " , ";
}
cout << endl;
}

int main()
{
int N;
int* p = NULL;
createDynamic(p,N);
showArray(p,N);
delete [] p;
return 0;
}
OUTPUT:

Number of elements in the array:5


Enter 0 th element:12
Enter 1 th element:45
Enter 2 th element:78
Enter 3 th element:98
Enter 4 th element:65
12 , 45 , 78 , 98 , 65 ,

Process returned 0 (0x0) execution time : 6.510 s


Press any key to continue.

Page | 1
This C++ code demonstrates how to dynamically allocate memory for an array, populate it with user-provided values,
display the array, and then deallocate the memory. Let's break down the code step by step:

createDynamic Function:

This function is responsible for creating the dynamic array. It takes two arguments:

 int*& p: A reference to an integer pointer. This is crucial. Because it's a reference, the function can
modify the pointer p in main so that main's p points to the newly allocated memory.
 int& N: A reference to an integer. This allows the function to modify the size N in main.

 It first prompts the user to enter the desired number of elements for the array and stores it in N.
 p = new int[N]; This is the core of dynamic memory allocation. new int[N] allocates a block of memory
large enough to hold N integers. It returns a pointer to the beginning of this block. This pointer is then assigned
to p.
 The for loop then iterates N times, prompting the user to enter each element of the array and storing it in the
allocated memory. p[i] accesses the i-th element of the dynamically allocated array.

showArray Function:

 This function takes the dynamically allocated array (pointed to by p) and its size N as input.
 It iterates through the array and prints each element followed by a comma and a space.
 Finally, it prints a newline character (endl) to move the cursor to the next line.

main Function:

 int N;: Declares an integer variable N to store the size of the array.
 int* p = NULL;: Declares an integer pointer p and initializes it to NULL. This is good practice to
indicate that the pointer doesn't point to anything initially.
 createDynamic(p, N);: Calls the createDynamic function, passing p and N by reference. This is
where the dynamic array is created and p is made to point to it. N is also set by the user's input.
 showArray(p, N);: Calls the showArray function to display the contents of the dynamically allocated
array.
 delete[] p;: This is extremely important. It deallocates the memory that was allocated by new
int[N]. Failing to do this will result in a memory leak. The [] is crucial because we allocated an array
(using new int[]).
 return 0;: Indicates that the program executed successfully.

Key Concepts:

 Dynamic Memory Allocation: The new operator is used to allocate memory at runtime. This is
essential when you don't know the size of the array at compile time.
 Pointers: Pointers store memory addresses. They are used to access and manipulate dynamically
allocated memory.
 References: References provide an alias for a variable. They allow functions to modify the original
variable passed as an argument. This is how createDynamic can change p and N in main.
 Memory Leaks: If you allocate memory using new but don't deallocate it using delete, that memory
becomes inaccessible to your program, leading to a memory leak. This can cause your program to run
out of memory and crash. Always deallocate dynamically allocated memory.

Page | 2

You might also like