What about that exception on one platform and not another for mystring(0) ?
Or, for that matter, the exception that that code throws sometimes but
not other times on a single platform, within the same execution? Or
that that code always throws on a given platform, when run under high
load, but never throws on the same platform under low load? Or any of
a number of other circumstances?
What ? malloc(23) should only return null in the case of a bug (non
conformance) or inability to allocate memory (bad_alloc). Are you
advocating otherwise ?
The C standard, from whence malloc comes, is extremely conservative
when it comes to defining what standard library functions that have
side effects are required to do. The verbiage in C99 is as follows:
If the space cannot be allocated, a null pointer is
returned.
It does not say "If the space is unavailable", or "If memory is full",
or anything else, merely "if the space cannot be allocated." As I
said: it's a quality of implementation issue; a sufficiently poor
implementation may never be able to allocate exactly 23 bytes and
unwilling to allocate more than requested. Such an implementation
would be conformant, even though it would likely have no users.
The standard does continue on to describe what appears to be a special
case for malloc(0):
If the size of the space requested is zero, the behavior is
implementation-
defined: either a null pointer is returned, or the behavior is as if
the size
were some nonzero value, except that the returned pointer shall not
be used
to access an object.
However, on closer analysis both of these cases can be derived from
the general behaviour of malloc described elsewhere: 1. that
arithmetic on the returned pointer be limited to the half-open
interval [x, x+size), which is an empty interval when size is 0, and
2. that malloc may return NULL. Therefore this is not a special case,
and implementations that return NULL on malloc(0) even when memory is
available are merely poor implementations.
Isn't it time to get these implementations fixed, Gianni?