I think there are two different things going on.

(1) In your contrived example, the malloc/free calls are optimized away 
entirely by the compiler. First the compiler optimizes away the “data != NULL” 
test, replacing it with “true” on the assumption malloc can’t fail; then it 
sees that there are no remaining uses of “data” so it optimizes out the malloc 
and free calls.

I think you’ve found an edge case here — in general malloc will not return NULL 
whether or not it can supply that much memory (see below), however it appears 
that given a sufficiently ridiculous size (you asked for something like 150 
terabytes!) it _will_ return NULL. I don’t know how “sufficiently ridiculous” 
is determined; maybe there’s a hardcoded limit?
        I am not a compiler engineer, but my guess is that optimizing away 
mallocs is sufficiently useful in real code that they’ve decided to ignore the 
edge case where someone passes a ridiculously large size.

(2) In your real code, which you said crashes, what’s probably going on is that 
malloc is overcommitting — it allocates as much address space as you asked for, 
but doesn’t map it to any actual RAM or backing store. Then as the address 
space is used, the page faults trigger allocation of actual RAM and backing 
storage, probably by growing the swap file. At some point it becomes unable to 
allocate (probably because the boot disk filled up?), and the page-fault fails 
with a segfault.

Operating systems behave differently with regard to overcommitting. I’m not 
very familiar with Linux and almost totally unfamiliar with Windows. My 
understanding is that Linux has some kind of “OOM Killer” process that will 
kill any process that’s using too much memory; presumably this happens before 
that process would run out of allocatable space. At the other extreme, iOS 
doesn’t use swap space at all and will kill a process that tries to use too 
much of physical RAM.

I don’t know what the best way is to ask for huge amounts of address space such 
that it’s all pre-mapped and can all safely be used without segfaulting. There 
may be an option to vm_allocate that does this. (Calling calloc won’t help; for 
large allocations that fall through to vm_allocate, calloc and malloc are 
equivalent.) You may want to ask on the darwin_userlevel or darwin_dev mailing 
lists.

—Jens
 _______________________________________________
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