[comp.std.c] SIZE_T_MAX

flee@guardian.cs.psu.edu (Felix Lee) (07/06/90)

Without something like SIZE_T_MAX, it can be hard to write portable
programs that behave sensibly on various overflow conditions.  Here
are some innocuous statements that can run into trouble, especially
when size_t is 16 bits:
	s = malloc(strlen(a) + strlen(b) + 1);
	c = n * sizeof(x);

I'd also like to have PTRDIFF_T_MIN and PTRDIFF_T_MAX and WCHAR_T_MAX
and....  To bad there isn't something similar to Ada's INTEGER'LAST.
--
Felix Lee	flee@cs.psu.edu

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/08/90)

In article <Fq7k90?@cs.psu.edu> flee@guardian.cs.psu.edu (Felix Lee) writes:
>when size_t is 16 bits:

size_t is obliged to be large enough to accommodate the possible data objects.

karl@haddock.ima.isc.com (Karl Heuer) (07/09/90)

In article <Fq7k90?@cs.psu.edu> flee@guardian.cs.psu.edu (Felix Lee) writes:
>Here are some innocuous statements that can run into trouble...

To test whether (expr <= SIZE_T_MAX), where (expr) is unsigned long int, use
(expr == (unsigned long int)(size_t)expr).  In your example,
	unsigned long int sz = 1UL + strlen(a) + strlen(b);
	s = (sz != (unsigned long int)(size_t)sz ? NULL : malloc((size_t)sz));

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint