-1

I have the following case.

int *arrayP=new int[4];
arrayP[0]=0;arrayP[1]=1;arrayP[2]=2;arrayP[3]=3;
int *temp;
cout<<arrayP[0]<<endl;
temp=arrayP;
++arrayP;
cout<<arrayP[0];
cout<<arrayP[1];
delete temp;
cout<<arrayP[0];

Output

0
1
2
-1212222

Why is it so? arrayP points to the first element on the array of size 4. Thus temp also points to the first element Now why deleting the temp i.e first element also deletes the entire integer array.arrayP does store the address of the next element of the array and hence the array is not orphan.(I assume it has deleted the integer array as arrayP[0] is showing garbage value.)

If such is the case(i.e deleting temp will delete the entire integer array) whats the difference between delete[] and delete.

ST3
  • 8,826
  • 3
  • 68
  • 92
user2511713
  • 555
  • 3
  • 7
  • 18
  • 3
    Now why deleting the temp i.e first ele— stop right there. You cannot use `delete` something you didn't allocate with `new`. If you use `new[]`, you must use `delete[]`. Once you violate this rule everything stops making sense. That said, why would anyone use `new[]` :| – R. Martinho Fernandes Jul 04 '13 at 14:48
  • Ok.So suppose my code doesnot need the first element of array anymore,there is no way to free that space.(I want to avoid copying it to a shorter array as its a time taking job and abviously link list) – user2511713 Jul 04 '13 at 14:52
  • If it doesn't need the first element of the array but needs the other ones then just don't refer to it; do you really care? – Bathsheba Jul 04 '13 at 14:55
  • @Borgleader:Sorry if the question offended your high tech brain.But most of the things are discovered because of that attitude only.Read a story about stephen hawking.You will understand.:). – user2511713 Jul 04 '13 at 14:58
  • @Bathsheba:Its taking up my memory and increasing my database size.How cant I care about it? – user2511713 Jul 04 '13 at 14:59
  • @user2511713 [Use the swap trick](https://ideone.com/Y43Pt3) – Borgleader Jul 04 '13 at 15:04
  • 2
    @user2511713 There is no logic *by definition* here. Once you delete sometihng you allocated with `new[]` the behaviour is undefined. *That's what the specification says*. His assertion that there is no logic to discover is a *spot-on technical remark*, and nothing for anyone to be offended over. – R. Martinho Fernandes Jul 04 '13 at 15:06
  • @Borgleader: [The swap trick is not guaranteed to work](http://stackoverflow.com/q/7829018/560648). – Lightness Races in Orbit Jul 04 '13 at 15:08
  • lol @R.MartinhoFernandes Thanks for clearing that.Borgleader: Sorry my bad – user2511713 Jul 04 '13 at 15:08
  • 1
    use a standard container instead of an array. std::deque seems to be what you want. You may want to look at boost.circular_buffer as well. – dhavenith Jul 04 '13 at 15:10
  • @Borgleader:internally the swap trick has O(n) i guess which I wanted to avoid. – user2511713 Jul 04 '13 at 15:14
  • @user2511713 You'd be surprised how quickly memory copies happen on modern hardware. If space is such a concern you may want to do some tests and benchmark it to see if it's worth it or not. – Borgleader Jul 04 '13 at 15:15

1 Answers1

8

If you use new[] you must balance it with delete[].

If you use new you must balance it with delete.

Mixing them will cause memory leaks / program crashes (technically undefined behaviour). (In your example, C++ is recording internally that you've created 4 ints; calling delete[] allows C++ to access this record. Calling delete does not).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483