Declarations of Array:
• Static array means the size of an array is static I.e; fixed
• Dynamic array means the size of an array is Dynamic I.e; flexible
• When an array is created it is created inside stack memory
• The size of the array is decided during at compile time
• When declaring an array it must be a static value only and not variable type in c language however in c++ dynamic allocation is possible during compile time
We can create array inside Heap
• When accessing any value inside a heap it must be done through a pointer
Again,
#include<iostream>
using namespace std;
int main()
{
int *p;
p = new int [5];
p[0]=12;
p[1]=13;
cout << p[0]; //output : 12
cout << p[1]; //output : 13
cout << p[2]; //output : harbage
return 0;
}
Create a new array of a bigger size and transfer elements of old array to this new array.
Here the array of pointer is in stack but the actual array is in heap.
Here we'll put everything in heap now.