In most cases, sockaddr
is not a base class of sockaddr_in
(source 1, 2, 3):
struct sockaddr {
ushort sa_family;
char sa_data[14];
};
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
Therefore according to [expr.delete]/p3 the behavior of the program is undefined:
... if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.
With that said, in many cases the built-in operator delete
simply delegates to free
, 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 and delete
the sockaddr_in
upon destruction?