#include<iostream>
using namespace std;

int main()
{
  int* p=new int[2];
  p[0]=1;
  p[1]=2;
  cout<<p[0]<<" "<<&p[0]<<endl;
  cout<<p[1]<<" "<<&p[1]<<endl;
  cout<<endl;
  cout<<*p<<" "<<p<<endl;
  p++;
  cout<<*p<<" "<<p<<endl;
  delete [] p;
}

the output of the above program is :
1 0xe502f0
2 0xe502f4

1 0xe502f0
2 0xe502f4

aborted(core dumped)
can anyone pls tell me why the program gives an aborted error...

If your going to increment your pointer then save its original value somewhere and delete that.

Edited 5 Years Ago by gerard4143: n/a

can you please tell me why i am getting the aborted error...
have i written the above program in a wrong way??

The memory manager is choking on your pointer 'p' because you incremented its value.

Check the enclosed code..

#include<iostream>
using namespace std;

int main()
{
  int* p=new int[2];
  p[0]=1;
  p[1]=2;
  cout<<p[0]<<" "<<&p[0]<<endl;
  cout<<p[1]<<" "<<&p[1]<<endl;
  cout<<endl;
  cout<<*p<<" "<<p<<endl;
  p++;
  cout<<*p<<" "<<p<<endl;
  p--;//set pointer back to its original value
  delete [] p;
}

Edited 5 Years Ago by gerard4143: n/a

This question has already been answered. Start a new discussion instead.