Jack Klein said:
A unsuffixed decimal integer constant will never be treated as an
unsigned type, no matter what its value. Prior to C99, the result is
undefined if a decimal integer constant has a value larger than
LONG_MAX. Under C99, it the value of the constant is greater than
LLONG_MAX, it may be converted to an extended integer type if and only
if the implementation has a signed extended integer type large enough
to hold its value.
C99 6.4.4.1 P5.
The original question was how to allocate 50 kilobytes if int is only
16 bits (so INT_MAX==32767).
If you use a single integer constant such as 50000 or 51200, it's
guaranteed to be of some type able to hold its value, and, assuming
the prototype is visible, it will be implicitly converted to size_t in
malloc(51200).
But the significance of the number 51200 isn't entirely obvious at a
glance, so it's clearer to use an explicit multiplication:
malloc(50 * 1024)
But both of these constants are of type int, and if INT_MAX==32767 the
overflow will cause undefined behavior.
jacob suggested using calloc(), but that's wasteful if you don't need
zero-initialization. It avoids overflow only because calloc() just
happens to take two operands. In another example that jacob
presented, if you want to allocate 50*1024 ints rather than bytes, you
have *three* numbers to multiply, and the calloc() trick falls down.
In real life, you're not likely to be using a simple magic number for
this kind of thing. But if that's really what you need to do, this is
a rare case where a cast may be the right solution:
malloc((size_t)50 * 1024)
Note that it's important to cast only one operand; this:
malloc((size_t)(50 * 1024))
has the same problem as the version without the cast. If you prefer
symmetry:
malloc((size_t)50 * (size_t)1024)
The problem is that, though you can use a "U" suffix for unsigned, or
"UL" for unsigned long, there's no suffix for size_t.