The size_t
type is the unsigned integer type of the result of the sizeof
operator. The underlying representation of variables of type size_t
are guaranteed to be of sufficient precision to represent the size of an object. The limit of size_t
is specified by the SIZE_MAX
macro.
Any variable which that is used to represent the size of an object including, but not limited to, integer values used as sizes, indices, loop counters, and lengths should be declared as size_t
.
...
The user defined function calc_size
(not shown) is used to calculate the size of the string other_srtingstring
. The result of calc_size
is a signed int
returned into str_size
. Given that there is no check on str_size
, it is impossible to tell whether the result of calc_size
is an appropriate parameter for malloc, that is, a positive integer that can be properly represented by a signed int
type.
...
By changing str_size
to a variable of type size_t
, it can be assured ensured that the call to malloc()
is, at the least, supplied a non-negative number.
...