Behaviour of delete[] (ptr, 0)
Asked Answered
C

2

7

Is the behaviour of this code defined?

int* ptr = new int[10];
operator delete[] (ptr, 0);

This code compiles fine and (on my machine) it seems nothing is happening. Is its the behaviour defined somewhere?

Celio answered 26/7, 2019 at 12:29 Comment(7)
This operator delete and operator delete[] reference should be helpful. Exact which overload you're calling is hard to say though.Carlenacarlene
I think it's undefined behaviour. The comma operator evaluates ptr and discards the result, then passes 0 (an invalid address) to deleteSpeculation
@Speculation It's a function call. There is no comma operator here.Hege
@Speculation 0 is a valid address to delete. It is a null pointer literal.Gottlieb
@Hege I see that now. How can it take 2 arguments though?Speculation
@Gottlieb I (mistakenly) thought deleting a null ptr was equivalent to deleting memory that hadn't been allocated with new and therefore undefinedSpeculation
@Speculation They can have 3 arguments even! :) Have a look: en.cppreference.com/w/cpp/memory/new/operator_deleteHege
W
1

tl;dr: The behavior might as well be undefined, avoid such a statement.

This answer is aimed at most C++ software developers, not so much at language-lawyer types who want the formal official answer.

The delete[](ptr, 0) call has no strong reason for the standard committee to define behavior for. Also, even if such a behavior were to be defined - there is no single obvious behavior we would expect:

  • It could throw an exception.
  • It could do nothing.
  • It could be the same as delete[](ptr) or delete[](ptr, 10).
  • It could be even be 'defined' as implementation-defined...

It is not a good idea for your code to rely on such a choice, even if the standards committee had made it. It would make your code difficult to understand and easy to break - for somebody else, or even for yourself, a few years down the line.

That's why it doesn't really matter whether the behavior here is defined, and you should absolutely avoid such a statement.

See also :

Whereby answered 12/5, 2023 at 19:30 Comment(0)
D
0

In this statement

operator delete[] (ptr, 0);

there is called explicitly the deallocation function

void operator delete[](void*, std::size_t) noexcept;

The second parameter in the call that has the type size_t is just set to 0.

The behavior of the call then the second parameter is equal to 0 is undefined provided that when the allocated memory was not have the size equal to 0.

Dorsey answered 26/7, 2019 at 12:32 Comment(6)
It's an operator call? It's not the comma operator but the arguments to the operator.Hege
That would be delete[](ptr, 0) (without the operator keyword). Now the OP is calling the operator function directly.Carlenacarlene
@VladfromMoscow It's not better. There is no comma operator. f(1, 2) is not equivalent to f(2). Here f is operator delete[].Hege
@Hege Og, you are right. I will delete the post.:)Dorsey
Does it? Cppreference states that it Called instead of (1-2) if a user-defined replacement is provided and the OP has not mentioned overloading the operator.Hembree
Also the OP is asking if it is defined behavior. Is it defined behavior to give it an incorrect size?Hembree

© 2022 - 2024 — McMap. All rights reserved.