Creation and Deletion of Array Example in C++


Example Program : 1

The program is used to demonstrate dynamic memory allocation for arrays in C++.

  • The program first declares an integer variable n to hold the size of the array to be created.
  • It then creates a dynamic integer array using the new operator and assigns the memory address of the first element of the array to a pointer variable arr. The size of the array is determined by the value of n.
  • The program then prompts the user to enter the elements of the array one by one using a for loop.
  • After all the elements are entered, the program prints out the elements of the array using another for loop.
  • Finally, the program deallocates the memory occupied by the array using the delete operator.

However, there is an issue in this program. The size of the array is not initialized before it is used to create the array using dynamic memory allocation. This can cause undefined behavior as the value of n is indeterminate and may contain a garbage value. To fix this issue, the program should prompt the user to enter the value of n before allocating memory for the array.

Source Code

#include<iostream>
using namespace std;
int main()
{
  int n;
  int *arr=new int[n];
  cout<<"Enter the size of the array : ";
  std::cin>>n;
  cout<<"\nEnter the element : ";
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  cout<<"\nThe elements that you have entered are :";
  for(int i=0;i<n;i++)
  {
    cout<<arr[i]<<",";
  }
  delete arr;
  return 0;
}
To download raw file Click Here

Output

Enter the size of the array : 4
Enter the element : 12
13
14
14
The elements that you have entered are :12,13,14,14,

Example Program : 2

This program takes in the total number of students and their respective GPAs as input from the user using dynamic memory allocation. It then displays the GPAs entered by the user. Here's a step-by-step explanation of the program:

  • The program prompts the user to enter the total number of students.
  • It then creates a pointer variable ptr of type float using dynamic memory allocation, allocating memory to store num number of float variables.
  • The program then prompts the user to enter the GPAs of each student using a for loop that iterates num number of times. In each iteration, it prompts the user to enter the GPA for the current student, and stores the value at the memory location pointed to by ptr+i.
  • After all the GPAs have been entered, the program displays them using another for loop that iterates num number of times. In each iteration, it displays the GPA for the current student by dereferencing the memory location pointed to by ptr+i.
  • Finally, the memory allocated to ptr is freed using the delete[] operator.

Overall, this program demonstrates the use of dynamic memory allocation and pointer arithmetic to store and access data in memory.

Source Code

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
  int num;
  cout<<"Enter total number of students: ";
  cin>>num;
  float* ptr;
  ptr = new float[num];
  cout<<"Enter GPA of students."<<endl;
  for (int i=0;i<num;++i)
  {
    cout<<"Student"<<i+1<<": ";
    cin>>*(ptr+i);
  }
  cout<<"\nDisplaying GPA of students."<<endl;
  for (int i=0;i<num;++i)
  {
    cout<<"Student"<<i+1<<" :"<<*(ptr + i)<<endl;
  }
  delete [] ptr;
  return 0;
}
To download raw file Click Here

Output

Enter total number of students: 3
Enter GPA of students.
Student1: 89
Student2: 78
Student3: 98
Displaying GPA of students.
Student1 :89
Student2 :78
Student3 :98


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples