Dynamic Memory Allocation
Contents:
• Dynamic Allocation (1D, 2D)
• Pointer as a reference
Usman Anwer
• Shallow vs Deep Lecturer
Spring 2025
Dynamic Allocation
• To create/destroy variables during program execution.
• Asking the computer to set aside a chunk of unused memory to hold
a variable of a specific data type.
int * ptr = nullptr
iptr=new int
*iptr=25
cout<<*iptr
new Operator
iptr = new int [100] to allocate memory to 100 element array
for(int i=0;i<100;i++)
iptr[i]=1
If requested memory is not available, C++ throws an exception and
terminates the program.
new Operator
• Failure to release dynamically allocated memory can cause memory
leak.
• To delete dynamically allocated memory, use the following
statements:
delete iptr; // for a variable
delete [] iptr // for an array variable
iptr=nullptr
• Example program to calculate the average sales of number of days
entered by user.
Pointer as a reference
int x = 10
int y = 30
int *ptr=&x
int * & refToPtr=ptr
refToPtr=&y
*refToPtr=20
cout<<x<<endl;
cout<<y<<endl;
Reference pointers in Functions?
void pointerParameters(int * & p, double *q){
Can we change the value of q and *q?
Can er change the value of p and *p?
Dynamic 2D Arrays
• Consider the statement
int * board [4]
This statement declares board to be array of four pointers wherein
each pointer is of type int.
Can we use board[0], board[1], board[2], board[3] to create rows of
board?
Dynamic 2D Arrays
• Consider the statement
int * board [4]
This statement declares board to be array of four pointers wherein
each pointer is of type int.
Can we use board[0], board[1], board[2], board[3] to create rows of
board? Yes
for (int i=0;i<4;i++){
board[i]=new int[6];
} what is the size of 2D array now?
Dynamic 2D Arrays
• Consider the statement (METHOD 1)
int * board [4]
This statement declares board to be array of four pointers wherein
each pointer is of type int.
Can we use board[0], board[1], board[2], board[3] to create rows of
board? Yes
for (int i=0;i<4;i++){
board[i]=new int[6];
} what is the size of 2D array now? 4x6
2D arrays
• In the last slides, we created 2D array of 4x6.
• The number of rows were fixed. How to make it dynamic?
• Consider the statements (METHOD 2):
int ** board; // board to be a pointer to pointer
board=new int* [6]; //creates an array of 6 int pointers
for(int i=0;i<4;i++){
board[i]=new int[4]; // creates an array of 4 elements at
each pointer
}
Shallow vs Deep Copy
• Shallow Copy: Copies only the pointer, not the actual data. Both the
original and the copied pointer share the same memory. Any changes
made through one pointer affect the other.
• Deep Copy: Copies the actual data into a new memory location. The
original and copied pointer arrays have independent memory, so
changes to one do not affect the other.
Shallow Copy
// Original array
int* originalArray = new int[size]{1, 2, 3, 4, 5};
int* newArray = nullptr;
void shallowCopy(int* srcArray, int*& destArray, int size) {
destArray = srcArray;
}
Deep Copy
// Original array
int* originalArray = new int[size]{1, 2, 3, 4, 5};
int* newArray = nullptr;
void deepCopy(int* srcArray, int*& destArray, int size)
{ destArray = new int[size]; // Allocate new memory
for (int i = 0; i < size; ++i) {
destArray[i] = srcArray[i]; // Copy each element
}}
Find the output
int *intArrayPtr; int *temp; intArrayPtr = new int[5]; *intArrayPtr = 7;
temp = intArrayPtr;
for (int i = 1; i < 5; i++) {
intArrayPtr++;
*intArrayPtr = *(intArrayPtr - 1) + 2 * i; }
intArrayPtr = temp;
for (int i = 0; i < 5; i++) {
cout << *intArrayPtr << " ";
intArrayPtr++; }