Closed ohyeaah closed 3 years ago
This behavior has been there since the earliest days of the core, so I'm a little confused by the issue.
Could you please point out where in the C++ spec that new[0]
or malloc(0)
can't return nullptr
? And what is the specific problem if it does?
You should be able to call any non-virtual members of the object as long as they're static
, but by definition you're not going to be able to use anything via the this
(i.e. member variables, vtable functions, non-static fcns, etc.).
It's not a big deal to add a if (!size) size=1;
to the malloc
wrapper, but without a good reason it's hard to justify opening up that code...
See also Stackoverflow - C++ new int[0] -- will it allocate memory?
I think it should return a nullptr, simply because behavior is undefined if you try to dereference the returned pointer.
If your code needs to work also with allocating nothing but basing its success on the pointer not being a nullptr, I guess you should have another look at that code. (or am I missing your point here?) If however the allocation is throwing an exception, then I guess you have a point here, but I don't think it is doing that, right?
Basic Infos
Platform
Settings in IDE
Problem Description
The problem is malloc(0) returns 0. I think this is not valid C nor CPP. Because of this new CLASS[0] becomes 0 when returned in a function (but not if used as a local value). That's not valid CPP behaviour. I am very sure new CLASS[0] may never be 0.
MCVE Sketch
Debug Messages