Will delete free the right amount of bytes ?
unique_ptr<sockaddr_in> up = make_unique<sockaddr_in>();
// or unique_ptr<sockaddr_in> up( new sockaddr_in ); ???
/*
Some stuff
sockaddr and sockaddr_in are two different types of struct and are not relateted
*/
sockaddr *p = reinterpret_cast<sockaddr *>( up.release() );
delete p;
In most cases,
sockaddr
is not a base class ofsockaddr_in
(source 1, 2, 3):Therefore according to [expr.delete]/p3 the behavior of the program is undefined:
With that said, in many cases the built-in operator
delete
simply delegates tofree
, which does not need the size, and the program will appear to work as intended (though when coding in C++ we really prefer to stay in the realm of defined behavior).Why not simply let the
unique_ptr
do its job anddelete
thesockaddr_in
upon destruction?