Hmm

behavior you described is good receipt to un-robust software

IMHO, all talks about “effective", “typically" and so on and on is matter of 
interpretation

and are much less important than standard

first of all there is a standard and nothing more. In some cases standard says 
that behavior is undefined

but in case of “malloc" and "operator new” everything is well defined

I agree that case I provided is very artificial, but we have real use case, 
where clang’s malloc returns not nullptr

and after that our product crashes (it’s MATLAB by the way)

g++ 4.9.2 and VS2013 behave as expected. In many cases clang is much closer to 
standard, 

but case we’re discussing isn’t one of them

and our customers won’t be very happy to see their MATLAB session crashes if 
they try to allocate more memory than

their computer can handle. I’m sure they would prefer to have error message and 
unaffected MATLAB session

in that particular case replacing malloc by std::vector effectively ( :))) ) 
solved the bug,

because std::vector’s ctor  just throws exception for debug or release build


thanks

dm




> On Jul 4, 2016, at 6:26 PM, Clark Cox <clarkc...@gmail.com> wrote:
> 
> 
> 
> 
>> On Jul 4, 2016, at 13:58, Dmitry Markman <dmark...@mac.com> wrote:
>> 
>> Hi Clark, FYI
>> 
>> http://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf
> 
> I am *well* aware of what the standard says. 
> 
>> 
>> 7.20.3.3 The malloc function
>> . . . . .
>> Description
>> 2 The malloc function allocates space for an object
>> whose size is specified by size and whose value is indeterminate.
>> Returns
>> 3 The malloc function returns either a null pointer 
>> or a pointer to the allocated space.
>> 
>> so there is nothing in C standard about never returning NULL
> 
> Also note that there is nothing in the standard about *requiring* that NULL 
> be returned. On a modern, UNIX system malloc will typically “succeed” and 
> return a pointer to a not-yet-paged in block of memory. As far as the C 
> standard is concerned, that malloc succeeded in allocating a block of memory. 
> However, when you later go and access that memory, and the OS realizes that 
> it can’t handle the resulting page-fault, your program crashes. This is a 
> long-time, and well understood issue; and in light of that behavior, the 
> compiler is 100% correct to do what it is doing.
> 
>>> On Jul 4, 2016, at 3:58 PM, Clark Cox <clarkc...@gmail.com> wrote:
>>> 
>>> Malloc effectively *never* returns NULL.
> 
> Notice the word “effectively”.
> 
> -- 
> Clark Smith Cox III
> clarkc...@gmail.com
> 

Dmitry Markman


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (Xcode-users@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to