3 ms·
I also wondered quite a bit about this difference. One interesting advantage of this alternate API is that calloc can always check for overflows, since a common
by amscanne 4y ago
I also wondered quite a bit about this difference. One interesting advantage of this alternate API is that calloc can always check for overflows, since a common pattern might be “calloc(user_input, sizeof(type))”, where using the older API without an overflow check on the multiplication would be perfect for constructing a buffer overflow.
With respect to your explanation, I imagine there are some cases (maybe something older?), but in my experience the norm for an int on a system with 32-bit pointers is also 32-bits.
- ksherlock 4y ago8086 has 16-bit registers and 16-bit near pointers but there are also 32-bit far pointers using the segment register. PDP-11, where calloc most likely originated, had 16-bit integers and 16-bit pointers. Apparently, later PDP-11s could be split into 64k of code space and 64k of data space (like the Harvard architecture) but that would still use 16-bit pointers.