Dynamic Arrays in C++
What is a Static Array?
A static array is an array whose size is fixed at compile time.
● Once you declare it, you cannot change its size while the program is
running.
● Every element is stored contiguously (next to each other in memory).
Code Example of static Array:
Output:
What is a Dynamic Array?
A dynamic array is an array whose size can be decided at runtime and can grow
or shrink while the program runs.
Unlike a static array (fixed size, stack memory), a dynamic array is created on
the heap using a pointer and memory management operators (new, delete).
Simple Beginner Friendly Dynamic array input code:
Program to dynamically allocate memory and take input from the user and
show output to it.
NOthrow:
So what we can do to catch the error if a user enters a positive trillion size
array size. We can use NOthrow but how it works.
If code throws an error on size of array no throw will catch it automatically
and it will assign pointer to null.But it will still execute program further
why??
Lets test it:
If you run the below program even though you have entered 1 trillion can
my pc process it?typically no but Operating system will force the program
to run it,it will show Memory is allocated but it will not process program
further because my system does not have enough memory to process it
so,NOThrow is catching error here without catch block and without
crashing the program it successfully cannot process further because of
memory issue.So nothrow catches error and prevents program from
crashing without try catch block.
Output:
Program to declare an array size and save its input without declaring or
asking from user using dynamic arrays.
Lab tasks:
1.Write a C++ program to demonstrate the use of static and dynamic arrays.
1. Declare and initialize a static array of integers with at least 5 elements.
2. Write a function called doubler that:
○ Takes the static array and its size as input.
○ Creates a dynamic array of the same size using the new operator.
○ Stores in the dynamic array values that are double the values of the static
array.
○ Returns the pointer to the dynamic array.
3. In the main() function:
○ Print the elements of the static array.
○ Call the doubler function to get the dynamic array.
○ Print the elements of the dynamic array.
○ Free the memory of the dynamic array using delete[].
2.Write a C++ program that asks the user for the size of an array.
● If the user enters a negative size, the program should handle the error using
exception handling (try–catch) without using nothrow.
● If the size is valid, create a dynamic array using the new operator, allow the user to
input elements, and then display them.
● Finally, release the memory using delete[].
3.Write a C++ program that:
1. Defines a struct Numbers with:
○ int *data (pointer to dynamic array)
○ int size (size of array)
2. Initializes a static array {1, 2, 3, 4, 5}.
3. Allocates a dynamic array in the struct and copies double the values of the static
array.
4. Print both arrays.
Q4:Write a C++ program that demonstrates copying between two dynamic arrays.
1. Ask the user for the size of the array.
2. Dynamically allocate two arrays:
○ arr1 → to store user input.
○ arr2 → to copy values from arr1.
3. Copy all values from arr1 to arr2.
4. Modify arr1 so that each element becomes double its original value.
5. Print the contents of both arrays.
6. Free the memory using delete[].