On 5 Jul 2016, at 08:33, Dmitry Markman <
email@hidden> wrote:
that is little troubling
if standard says that malloc returns NULL on failure and implementation doesn’t do that then to me it’s standard violation and is the direct way to troubles
No it’s not a standards violation. malloc() doesn’t fail, it gets a pointer to memory from the underlying VM system which you can attempt using, that pointer is never NULL. Failure is deferred to later if the memory isn’t actually available when you come to use it and then you get a crash at that point.
You can argue the VM system is too optimistic, however you can come up with use cases in which allocating ridiculous amounts of memory and using little bits of it at a time manage to work, or at the other extreme allocating one byte which isn’t actually used for days, and when it is, crashes because there’s not a single usable page left. So it’s a design decision of the VM system to always say OK and worry about fulfilling the request later. malloc() can do no more than trust the VM system it’s asking for memory from.
So you could file a bug saying the VM system is too optimistic always returning valid memory and then failing later, showing a use-case in which it should be able to know it won’t actually be able to provide the memory later. malloc() is standards compliant, the optimiser is correctly optimising away dead code, the VM system underlying the operating system is letting you down in this case, making a promise it cannot fulfil.
as I clearly see in my use case when MATLAB crashes
So a different approach is, since you know all the bits of memory you’re allocating all need to be used all at the same time, which is information the VM doesn’t have, is there a way to allocate memory not using malloc(), or not using the builtin version of malloc, which will only allocate this chunk of memory if you will be able to swap it all in all at once and have it all resident at once? I don’t know.
dm
No there isn’t anything about optimisation in the standards because all the standard says it that malloc returns NULL on failure. If an implementation of malloc is defined never to fail,it never returns NULL.