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

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by indr
0

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

Edited by gerard4143: n/a

0

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

2

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 by gerard4143: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.