Hello,
I am trying to allocate an array, store some int values in it, and then print them out.
Code:
#include <iostream>
using namespace std;
int main()
{
int size;
cout<<"\nEnter no. of variables:";
cin>>size;
int *ptr=new int[size];
for(int i=0;i<size;i++,ptr++)
{
cout<<endl<<"Value of ptr["<<i<<"]=";
cin>>*ptr;
}
ptr-=size;
for(int i=0;i<size;i++,ptr++)
cout<<endl<<"ptr["<<i<<"]="<<*ptr;
delete[] ptr;
return 0;
}
It is giving output like this:
Code:
Enter no. of variables:3
value of ptr[0]=56
value of ptr[1]=89
value of ptr[2]=4
ptr[0]=56
ptr[1]=89
segmentation fault (core dumped)
-----------------------------------------------
Program exited with code:139
Press return to continue
Please point out what's wrong in the code and what is segmentation fault ?
Thank you.