delete not destroying pointer...

hello,
anyway my issue is with gcc4.x, if this is the appropriate forum. i'm trying to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cstdlib>
using namespace std;

int main() {
  int* p = new int(5);
  *(p+1) = *(new int (6));
  cout << p << endl;
  cout << *p << endl;
  cout << (p+1) << endl;
  cout << *(p+1)<< endl;
  delete (p+1);
  delete p;
  cout << p << endl;
  cout << (p+1) << endl;
  cout << *p << endl;
  cout << *(p+1) << endl;
}

which i can do with the xlc compiler on a aix. the output from xlc:

1
2
3
4
5
6
7
8
9
[xxx] $ xlc++ p0.cpp
[xxx] $ ./a.out
20003448
5
2000344c
6
20003448
0
0


and the output of gcc:

1
2
3
4
5
6
7
8
9
10
pos# g++ p0.cpp
pos# ./a.out
0x28301098
5
0x2830109c
6
0x28301098
0x2830109c
5
6

any help would be great. thanks.

w.
You have to declare the p array with brackets.
int* p = new int[5]

To delete a pointer of arrays you have to use
delete[] p;

Look at this:
http://www.cplusplus.com/doc/tutorial/dynamic.html
Topic archived. No new replies allowed.