Mail Archives: djgpp/2000/05/19/05:45:33
Alexei A. Frounze <alex DOT fru AT mtu-net DOT ru> wrote:
> Suppose we have a string. If the string is definetely shorter than INT_MAX
> and SSIZE_MAX (say less than 32000 characters), there must be everything
> okay with type casting for strlen(), strncpy(), etc.
*If*. But in general, you can never know that it's shorter than 32000
characters. Especially not if you're just calling strlen() to find out
how long it is. Casting the return value of strlen() to int would mean
that you never learn how long that string actually was.
The problem with the type cast is that you cannot know wether it was
safe, after you've done it: you're throwing away information that may
have been vitally important.
Assuming that all strings are shorter than INT_MAX is a bad idea. You
should not let yourself get into the habit of making that
assumption. It may be true for the program you're just writing, where
you know how big all the strings can possibly be. But once you start
writing your programs that way, sooner or later you will introduce a
bug in one of your programs, because you made that assumption where it
was *not* true. And you'll have a very hard time finding that bug,
because your way of thinking, by then, will have been fixed. You'll
have forgotten that size_t would have been the cleaner solution.
After all: what *is* so bad about size_t in your opinion that makes
you try to avoid it like the plague? Using it you'ld have one nasty
detail less to worry about.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -