7

I read that delete[] can deallocate an array of objects. However it is not mentioned in any of the sources I've read that whether it is an error or undefined to supply an argument like delete[3].

I have the following queries.

  1. Is it specified in C++ standard whether I can/cannot suplly a parameter to delete[] as delete[3]?
  2. If yes, what is the effect?
  3. Also is it specified in C++ whether I can/cannot use delete for an array allocated from new[]?
pasha
  • 2,035
  • 20
  • 34
  • 3
    Sorry for this question but why would you like to give an argument to delete ? – John_Sharp1318 Feb 11 '16 at 12:18
  • 2
    Don't confuse it with `delete array[3]` which delets whatever `array[3]` points to (so if you have an array of pointers), and doesn't delete `array` or some part of it. Deleting a subarray is not possible, if you want to do that. – leemes Feb 11 '16 at 12:20
  • To write the function 'void move_from_array(int* target, int* source) { *target = *source; delete[1] source++; /* or simply delete source++ */ target++; }' – pasha Feb 11 '16 at 12:23
  • @leemes ...To do that first I have to allocate each element of array separately, right? which is more difficult than allocating using new[] – pasha Feb 11 '16 at 12:28
  • You're confusing something here. You can only delete a whole array, not part of it. So if you want to move contents, first allocate a new array, then move, then delete the old. Not element for element, that wouldn't work (if that would be possible, the allocated items are spread all over your memory, not contiguous as with in an array.) – leemes Feb 11 '16 at 12:32
  • Think of an array like a big bookshelf with a predefined number of slots for books. If you got rid of some books and want to save space in your room, you would want the bookshelf to shrink in size. Suppose you're bad at handcraft, you can't make the bookshelf smaller. You would have to buy a smaller one first, move the books over, then disassemble and destroy/sell the big bookshelf (if not needed anymore). You can't destroy one part of the bookshelf without touching the rest. Destroying a book is`delete array[3]`, for the bookshelf it's`delete[] array`. I hope this analogy is somewhat helpful. – leemes Feb 11 '16 at 12:39

3 Answers3

11

(1) Yes, it specifies you can't.

(3) It specifies the outcome is undefined, so don't.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
4

delete[] is an operator.

delete[x] is not a valid c++ syntax, so the code wont compile.

Praveen
  • 8,945
  • 4
  • 31
  • 49
  • So what you are saying is 'delete[]' is like a function name 'functionxyz' and what I'm trying to do is call it by 'function123'? – pasha Feb 11 '16 at 13:08
0

Yes, you can pass any argument with delete [x] operator, but it has no meaning. If you will mention any destructor in class then it will call destructor for infinite time. See below example

#include <iostream>
using namespace std;
class A
{
public: 
A() {
cout<<"\n Calling A constructor ";
}
/*~A(){
cout<<"\n Calling A Destructor ";
  }*/
};
int main()
{
A *a1= new A();

delete [3] a1;

return 0; 
}

This above program (test with VC++) compile and run successfully, if uncommented that destructor part then compile successfully but destrutor will call infinite time, Which is undefined behavior of the program.

Kulamani
  • 509
  • 6
  • 13