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

Dynamic Memory Allocaion

Uploaded by

2n6dhkjm6p
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 views11 pages

Dynamic Memory Allocaion

Uploaded by

2n6dhkjm6p
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/ 11

Data Structure and

Algorithms
Author: Hafiz Abdul Rehman
Instructor: Hafiz Abdul Rehman
Lecture 03 – Dynamic Memory Allocation
Dynamic Memory Allocation

• Dynamic memory allocation refers to allocating


memory at runtime, as opposed to compile-time.
• It allows us to create data structures whose size
is determined during program execution.
Static VS Dynamic Array
Static Array:
• A static array has a fixed size that is determined at compile time.
• You declare it by specifying the array's data type, name, and size.
int myArray[5]; // Declares an integer array with 5 elements
Dynamic Array (using pointers):
• A dynamic array's size can be determined at runtime, and it is typically
allocated on the heap using pointers.
• You need to use new operator to allocate memory for the array
int* dynamicArray = new int[10]; // Declares a dynamic integer array with 10
elements
// Don't forget to delete it when done to prevent memory leaks
delete[] dynamicArray;
Why Dynamic Arrays
int numbers[10];
int numbers[100];
int numbers[1000];
int numbers[10000];
int numbers[100000];
int numbers[??];
Dynamic Memory Allocation of Array
• Determine the Size of the array at runtime.

Example
Int size;
// Ask the user for the size of the array
std::cout << "Enter the size of the array: ";
std::cin >> size;
Int* numbers = new int[size]
Resizing of Dynamic Array
Why Dynamic Resizing is Necessary?
1. In many cases, the size of data structures is not known in advance.
2. Fixed-size arrays can be inefficient or impractical.

Dynamic Resizing Strategies


Discuss common strategies for dynamic resizing:
1. Doubling the array size when it's full.
2. Halving the array size when it's less than a certain threshold.
3. Using a growth factor for resizing.
Resizing of Dynamic Array (Expand)
int size = 2; // Initial size of the array
int* dynamicArray = new int[size]; // Create an initial array of size 2
int count = 0; // Keep track of the number of elements in the array
While (true){
// Ask the user to enter an element or exit
int element;
What if Array is full??
std::cout << "Enter an element (-1 to exit): ";
std::cin >> element;
if (element == -1) { break; // Exit the loop }
// Add the element to the array Do something here..
dynamicArray[count] = element;
count++;
}
Resizing of Dynamic Array
// Check if the array is full
if (count == size) {
// Create a new array with double the size
// Copy elements from the old array to the new array
// Delete the old array and update the pointer
// Update the size to the new size
}
Resizing of Dynamic Array
// Check if the array is full
if (count == size) {
// Create a new array with double the size
int* newDynamicArray = new int[size * 2];
// Copy elements from the old array to the new array
for (int i = 0; i < size; i++) {
newDynamicArray[i] = dynamicArray[i]; }
// Delete the old array and update the pointer
delete[] dynamicArray;
dynamicArray = newDynamicArray;
// Update the size to the new size
size *= 2;
}
Resizing of Dynamic Array (Shrink)
// Check if the array is below a threshold
if (count < size / 2) {
// Create a new array with half the size
// Copy elements from the old array to the new array
// Delete the old array and update the pointer
// Update the size to the new size
}
Resizing of Dynamic Array (Shrink)
// Check if the array is below a threshold
if (count < size / 2) {
// Create a new array with half the size
int* newDynamicArray = new int[size / 2];
// Copy elements from the old array to the new array
for (int i = 0; i < count; i++) {
newDynamicArray[i] = dynamicArray[i];
}
// Delete the old array and update the pointer
delete[] dynamicArray;
dynamicArray = newDynamicArray;
// Update the size to the new size
size /= 2;
}

You might also like