Can I supply argument to delete[] like delete[3]?
Asked Answered
L

3

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[]?
Lupulin answered 11/2, 2016 at 12:6 Comment(6)
Sorry for this question but why would you like to give an argument to delete ?Jacie
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.Cowcatcher
To write the function 'void move_from_array(int* target, int* source) { target = *source; delete[1] source++; / or simply delete source++ */ target++; }'Lupulin
@Cowcatcher ...To do that first I have to allocate each element of array separately, right? which is more difficult than allocating using new[]Lupulin
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.)Cowcatcher
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 isdelete array[3], for the bookshelf it'sdelete[] array. I hope this analogy is somewhat helpful.Cowcatcher
A
11

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

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

Adventitious answered 11/2, 2016 at 12:17 Comment(0)
R
4

delete[] is an operator.

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

Richard answered 11/2, 2016 at 12:32 Comment(1)
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'?Lupulin
E
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.

Exploit answered 11/2, 2016 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.