#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...

Recommended Answers

All 4 Replies

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

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;
}

thank you so much;))))))))))))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.