Re:Why Does Calloc Exist?
by AceJohnny2 on Sunday November 13, 2022 @03:37AM
(#33581212)
Maybe it's late but I don't get it. Can someone spell it out for me?
[ Parent
| on HN ]
If n != 0 and -1 cast to unsigned size_t divided by n is less than m. So that m times n would be bigger than size_tâs maximum value.
Edit: inverted meaning since check is for error, not for ok case.
You want to evaluate p in:
p = m n
If that is gonna overflow, p > SIZE_MAX.Then it would be that (by substitution)
m n > SIZE_MAX
so m > SIZE_MAX/n
except if n = 0. In the latter case, m can be as big as it wants--p will always evaluate to 0.And (size_t)-1 is a silly way to write SIZE_MAX (but it saves you a dependency).
Re:Why Does Calloc Exist?
by AceJohnny2 on Sunday November 13, 2022 @08:19PM
(#33589194)
Thanks! Indeed the `(size_t)-1` threw me off, and the `m && n` was an interesting obfuscation for checking whether either are 0.