As far as compiler optimizations go, is it legal and/or possible to change a heap allocation to a stack allocation? Or would that break the as-if rule?
For example, say this is the original version of the code
{
Foo* f = new Foo();
f->do_something();
delete f;
}
Would a compiler be able to change this to the following
{
Foo f{};
f.do_something();
}
I wouldn't think so, because that would have implications if the original version was relying on things like custom allocators. Does the standard say anything specifically about this?
Yes, it's legal. expr.new/10 of C++14:
An implementation is allowed to omit a call to a replaceable global
allocation function (18.6.1.1, 18.6.1.2). When it does so, the storage
is instead provided by the implementation or provided by extending the
allocation of another new-expression.
expr.delete/7:
If the value of the operand of the delete-expression is not a null
pointer value, then:
— If the allocation call for the new-expression for the object to be
deleted was not omitted and the allocation was not extended (5.3.4),
the delete-expression shall call a deallocation function (3.7.4.2).
The value returned from the allocation call of the new-expression
shall be passed as the first argument to the deallocation function.
— Otherwise, if the allocation was extended or was provided by
extending the allocation of another new- expression, and the
delete-expression for every other pointer value produced by a
new-expression that had storage provided by the extended
new-expression has been evaluated, the delete-expression shall call a
deallocation function. The value returned from the allocation call of
the extended new-expression shall be passed as the first argument to
the deallocation function.
— Otherwise, the delete-expression will not call a deallocation
function (3.7.4.2).
So, in summary, it's legal to replace new and delete with something implementation defined, like using the stack instead of heap.
Note: As Massimiliano Janes comments, the compiler could not stick exactly to this transformation for your sample, if do_something throws: the compiler should omit destructor call of f in this case (while your transformed sample does call the destructor in this case). But other than that, it is free to put f into the stack.
These are not equivalent. f.do_something() might throw, in which case the first object remains in memory, the second gets destructed.
I'd like to point out something IMO not stressed enough in the other answers:
struct Foo {
static void * operator new(std::size_t count) {
std::cout << "Hey ho!" << std::endl;
return ::operator new(count);
}
};
An allocation new Foo() cannot generally be replaced, because:
An implementation is allowed to omit a call to a replaceable global allocation function (18.6.1.1, 18.6.1.2). When it does so, the storage is instead provided by the implementation or provided by extending the allocation of another new-expression.
Thus, like in the Foo example above, the Foo::operator new needs to be called. Omitting this call would change the observable behavior of the program.
Real world example: Foos might need to reside in some special memory region (like memory mapped IO) to function properly.
Related
I've written the below code which overloads the new and delete operators and throws an exception in the destructor.
When the exception is thrown, why is the code in the delete operator not executed (and "bye" printed)?
If it shouldn't be executed, (how) is the memory freed? Is one of the other delete operators called? Would overloading one of them instead result in the corresponding code being executed? Or is the memory simply not freed because a failed destruction implies that maybe it shouldn't be?
#include <iostream>
using namespace std;
class A
{
public:
A() { }
~A() noexcept(false) { throw exception(); }
void* operator new (std::size_t count)
{
cout << "hi" << endl;
return ::operator new(count);
}
void operator delete (void* ptr)
{
cout << "bye" << endl;
return ::operator delete(ptr);
}
// using these (with corresponding new's) don't seem to work either
// void operator delete (void* ptr, const std::nothrow_t& tag);
// void operator delete (void* ptr, void* place);
};
int main()
{
A* a = new A();
try
{
delete a;
}
catch(...)
{
cout << "eek" << endl;
}
return 0;
}
Output:
hi
eek
Live demo.
I looked at:
throwing exceptions out of a destructor
How does C++ free the memory when a constructor throws an exception and a custom new is used
And others
But I couldn't find an answer to what exactly happens (1) for an exception in the destructor (as opposed to the constructor) and (2) with an overloaded delete.
I don't need a lecture on throwing an exception in a destructor being bad practice - I just ran into similar code and I'm curious about the behaviour.
I would prefer an answer supported by the standard or similar references, if such references exist.
The
standard Draft N4296 5.3.5, pag 121 says:
[expr.delete] [ Note: The deallocation function
is called regardless of whether the destructor for the object or some element of the array throws an exception.
— end note ]
So the operator delete has to be called regardeless the destructor throws.
However, as has emerged from the comments, some compilers does not properly call the operator delete. This can be resolved as bug compiler.
Bug tested for:
GCC 4.8
Visual Studio 2015
In the 1998 C++ standard (ISO/IEC 14882 First edition, 1998-09-01) the workings of a delete expression are stated quite simply in "Section 5.3.5 Delete [expr.delete]" in paras 6 and 7.
6 The delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in
reverse order of the completion of their constructor; see 12.6.2).
7 The delete-expression will call a deallocation function (3.7.3.2).
In combination, these clauses require that destructor will be invoked (or destructors for an array) and that the deallocation function will be called unconditionally. There is no provision here for not calling the deallocation function if an exception is thrown.
In the 1998 standard, language lawyers and compiler developers will probably take delight in the sophistry of arguing a different interpretation than I've stated above. Fortunately, things are more explicit in later standards...
In Draft N4296 available from open-std.org the same clauses are expanded as follows: (from memory the wording in the official standard is the same, but I don't have a copy on my current machine)
(emphasis mine)
6 If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an
array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see 12.6.2).
7 If the value of the operand of the delete-expression is not a null pointer value, then:
(7.1) - If the allocation call for the new-expression for the object to be deleted was not omitted and the allocation was not extended (5.3.4), the
delete-expression shall call a deallocation function (3.7.4.2). The value returned from the allocation call of the new-expression shall be passed as the first argument to the deallocation function.
(7.2) - Otherwise, if the allocation was extended or was provided by extending the allocation of another new-expression, and the delete-expression for every other pointer value produced by a new-expression that had storage provided by the extended new-expression has been evaluated, the
delete-expression shall call a deallocation function. The value returned from the allocation call of the extended new-expression shall be passed as the first argument to the deallocation function.
(7.3) - Otherwise, the delete-expression will not call a
deallocation function (3.7.4.2).
Otherwise, it is unspecified whether the deallocation function will be called. [Note: The deallocation function is called regardless of whether the destructor for the object or some element of the array throws an exception. — end note]
The note at the end spells out that the deallocation function must be called even if the destructor throws an exception.
I'm unsure offhand which evolution of the standard first spelled things out, but based on the above, the clauses will probably remain in Section 5.3.5 (tag [expr.delete]).
The destructor is called before calling to the delete operator. See cppreference - delete expression
If expression is not a null pointer, the delete expression invokes the destructor (if any) for the object that's being destroyed, or for every element of the array being destroyed (proceeding from the last element to the first element of the array).
After that, unless the matching new-expression was combined with another new-expression (since C++14) the delete expression invokes the deallocation function, either operator delete (for the first version of the expression) or operator delete[] (for the second version of the expression).
Due to this order of operations, the destructor is called and throws an exception before your overloaded version of the delete operator is called.
I know that an implementation should free any allocated memory if the constructor of an object throws an exception in situation like this:
new T(); // Suppose that T() throws an exception
but what about the following code?
new T(f()); // Suppose that T() does NOT throw any exception, but f() does
What should implementation do in this case? Should it free any allocated memory then?
In the current C++ standard (C++14, as well as in the previous versions C++11 and C++03), it is unspecified whether memory is allocated before or after f() is evaluated, but in any case memory will be freed if it has been allocated; [expr.new]:
20 - If any part of the object initialization described above79 terminates by throwing an exception, storage has
been obtained for the object, and a suitable deallocation function can be found, the deallocation function is
called to free the memory [...]
79) This may include evaluating a new-initializer and/or calling a constructor.
Here the new-initializer is f(), so if the evaluation of f() throws an exception, the deallocation function will be called (if found).
Since C++17, the allocation of memory is sequenced before the evaluation of f(), so the memory will always be deallocated:
21 - If any part of the object initialization described above79 terminates by throwing an exception and a suitable
deallocation function can be found, the deallocation function is called [...]
Note though that since memory allocation is elidable, the implementation is in practice free to omit the allocation if it can predict that an exception will be thrown.
The default placement new operator is declared in 18.6 [support.dynamic] ¶1 with a non-throwing exception-specification:
void* operator new (std::size_t size, void* ptr) noexcept;
This function does nothing except return ptr; so it is reasonable for it to be noexcept, however according to 5.3.4 [expr.new] ¶15 this means that the compiler must check it doesn't return null before invoking the object's constructor:
-15-
[Note: unless an allocation function is declared with a non-throwing exception-specification (15.4), it indicates failure to allocate storage by throwing a std::bad_alloc exception (Clause 15, 18.6.2.1); it returns a non-null pointer otherwise. If the allocation function is declared with a non-throwing exception-specification, it returns null to indicate failure to allocate storage and a non-null pointer otherwise. —end note] If the allocation function returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.
It seems to me that (specifically for placement new, not in general) this null check is an unfortunate performance hit, albeit small.
I've been debugging some code where placement new was being used in a very performance-sensitive code path to improve the compiler's code generation and the check for null was observed in the assembly. By providing a class-specific placement new overload that is declared with a throwing exception-specification (even though it can't possibly throw) the conditional branch was removed, which also allowed the compiler to generate smaller code for the surrounding inlined functions. The result of saying the placement new function could throw, even though it couldn't, was measurably better code.
So I've been wondering whether the null check is really required for the placement new case. The only way it can return null is if you pass it null. Although it's possible, and apparently legal, to write:
void* ptr = nullptr;
Obj* obj = new (ptr) Obj();
assert( obj == nullptr );
I can't see why that would be useful, I suggest it would be better if the programmer had to check for null explicitly before using placement new e.g.
Obj* obj = ptr ? new (ptr) Obj() : nullptr;
Has anyone ever needed placement new to correctly handle the null pointer case? (i.e. without adding an explicit check that ptr is a valid memory location.)
I'm wondering whether it would be reasonable to forbid passing a null pointer to the default placement new function, and if not whether there is some better way to avoid the unnecessary branch, other than trying to tell the compiler the value is not null e.g.
void* ptr = getAddress();
(void) *(Obj*)ptr; // inform the optimiser that dereferencing pointer is valid
Obj* obj = new (ptr) Obj();
Or:
void* ptr = getAddress();
if (!ptr)
__builtin_unreachable(); // same, but not portable
Obj* obj = new (ptr) Obj();
N.B. This question is intentionally tagged micro-optimisation, I am not suggesting that you go around overloading placement new for all your types to "improve" performance. This effect was noticed in a very specific performance-critical case and based on profiling and measurement.
Update: DR 1748 makes it undefined behaviour to use a null pointer with placement new, so compilers are no longer required to do the check.
While I can't see much of a question in there except "Has anyone ever needed placement new to correctly handle the null pointer case?" (I haven't), I think the case is interesting enough to spill some thoughts on the issue.
I consider the standard broken or incomplete wrt the placement new function and requirements to allocation functions in general.
If you look closely at the quoted §5.3.4,13, it implies that every allocation function has to be checked for a returned nullpointer, even if it is not noexcept. Therefore, it should be rewritten to
If the allocation function is declared with a non-throwing exception-specification and returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.
That would not harm the validity of allocation functions throwing exceptions, since they have to obey §3.7.4.1:
[...] If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. [...] The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement (3.11) and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function).
And §5.3.4,14:
[ Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. The block of storage is assumed to be appropriately aligned and of the requested size. [...] -end note ]
Obviously, a placement new that just returns the given pointer, cannot reasonably check avilable storage size and alignment. Therefore,
§18.6.1.3,1 about placement new says
[...] The provisions of (3.7.4) do not apply to these reserved placement forms of operator new and operator delete.
(I guess they missed to mention §5.3.4,14 at that place.)
However, together these paragraphs say indirectly "if you pass a garbage pointer to the palcement functions, you get UB, because §5.3.4,14 is violated". So it's up to you to check the sanity of any poitner given to placement new.
In that spirit, and with the rewritten §5.3.4,13, the standard could strip the noexcept from placement new, leading to an addition to that indirect conclusion: "...and if you pass null, you get UB as well". On the other hand, its much less likely to have a misaligned pointer or pointer to too few memory than having a null pointer.
However, this would remove the need for checking against null, and it would fit well to the philosophy "don't pay for what you don't need". The allocation function itself would not need to check, because §18.6.1.3,1 explicitly says so.
To round things up, one could consider adding a second overload
void* operator new(std::size_t size, void* ptr, const std::nothrow_t&) noexcept;
Sadly, proposing this to the committee is unlikely to result in a change, because it would break existing code relying on placement new being ok with null pointers.
Can anybody explain what will happen if a new is overloaded but corresponding delete is not loaded in C++?
This is only an issue when the object construction throws an exception, and it is described in C++11 5.3.4/18:
If no unambiguous matching deallocation function can be
found, propagating the exception does not cause the object’s memory to be freed. [ Note: This is appropriate
when the called allocation function does not allocate memory; otherwise, it is likely to result in a memory
leak. —end note ]
Example:
T * p = new (true, 'x', Blue) T("Jim");
If the constructor of T throws, we need an overload operator delete(void *, bool, char, enum Color), either at namespace scope or as a static member of T, and if this function does not exist, then no deallocation function is called.
As the note says, in the case of placement-new functions which are essentially no-ops, this may not be a problem. However, if the allocation function does non-trivial work, then there'll be no matching clean-up function.
guys! Out of curiosity – the following code would probably not be legal, would it?
T *p = ::operator new(sizeof(T)); // allocate memory for a T
new (p) T; // construct a T into the allocated memory
delete p; //delete the object using the standard delete operator
No. You can only delete what you get back from new- no exceptions.
There's at least one circumstance in which it's clearly undefined: if you've overloaded operator new and operator delete for T, then this will attempt to allocate memory using ::operator new, but delete it using T::operator delete. Unless your T::operator delete is purely a wrapper around ::operator delete, that's going to cause a problem.
Other than that, I think it's probably defined. The standard is very specific about the fact that a new expression allocates its memory using an allocation function (§5.3.4/10), which will be ::operator new as long as you haven't provided a T::operator new.
Your placement new expression then initializes the object, just as described for a new expression in the first bullet point of §5.3.4/15.
Then we get to the destruction side. According to $5.3.5/1: "The delete-expression operator destroys a most derived object (1.8) or array created by a new-expression." That requires that you have used a new expression to create the object -- which you have. You've used a placement new, which is one of the possibilities for a new expression, and specified in §5.3.4/1.
The next requirements that apply seem to be: "The operand shall have a pointer type, or a class type having a single conversion function (12.3.2) to a pointer type." Again, your expression meets that as well.
I'm going to quote more requirements, without further comment, except that your code seems to meet all of them (some limit the implementation of a delete expression, not the pointer you can use in one):
(§5.3.5/2): "In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If not, the behavior is undefined."
(§5.3.5/3): "In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.
(§5.3.5/4): "In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined."
(§5.3.5/6): "The delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted."
(§5.3.5/7): The delete-expression will call a deallocation function (3.7.3.2).
With the initial caveat about ::operator new vs. T::operator new, I think the pointer you're using in the delete expression meets all the requirements, so the behavior should be defined.
Having said all that, I certainly hope this is purely academic interest -- even though it looks to me like the code does have defined behavior, it's a lousy idea even at very best.
Going off of DeadMG's correct assertion, there is no problem with a slight change to your code:
unsigned char* addr = new unsigned char[sizeof(MySimpleStructure)];
MySimpleStructure* p = new (addr) MySimpleStructure;
delete [] addr;
Since we're deleteing addr which was returned by new there is no issue with this being legal. Of course, after addr is deleted, p should not be accessed (its a dangling pointer at that time). Also note that MySimpleStructure as allocated via placement new to p will not have its destuctor called.