3 ms·
malloc() will crash on too big input value, because malloc() takes unsigned size_t on input. Will it, or will it just fail and return null (which may crash lat
by userbinator 8y ago
malloc() will crash on too big input value, because malloc() takes unsigned size_t on input.
Will it, or will it just fail and return null (which may crash later code if it doesn't check)? I believe the spec mandates the latter.
- loeg 8y agoAre you suggesting C mandates operating systems not overcommit memory (I don't think it does), or that objects cannot be larger than PTRDIFF_MAX (and ptrdiff_t is signed)? (Edit: On further reading, I don't think the standard says that, either.) Many implementations do not conform to that definition. But it's also possible that the expression results in a value smaller than PTRDIFF_MAX (or similarly, small enough the operating system does not need to overcommit), and then even a malloc conforming to that definition is free to return a pointer to some virtual memory (committed or not).