Dynamic Memory Allocation With Pointer
Dynamic Memory Allocation With Pointer
CODE:
#include<iostream>
using namespace std;
int main()
{
int N;
int* p = NULL;
createDynamic(p,N);
showArray(p,N);
delete [] p;
return 0;
}
OUTPUT:
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