> Morten Welinder wrote:
> >
> > Oops, I didn't look to see what was inside that sizeof()! Anyway,
> > the idea is still sound as long as the two sizeof()s agree which,
> > I claim, is common.
> >
> > Morten
> > ("simplify the problem as much as possible, but no more than that"
> > -- freely after Einstein.)
>
> Your solution is close to being correct, POSIXwise. The completely
> "compliant" way of doing this is
>
> ssize_t read_return_value;
> size_t nr_of_bytes_read;
> ...
> read_return_value = read ( socket, buffer, sizeof buffer );
> if ( read_return_value == -1 ) {
> ... /* Handle the error. */
> }
> nr_of_bytes_read = (size_t) read_return_value;
Your cast is just stupid in my opinion.
Your are just assuming that the called function only returns > 0 when it
does not return -1.
You probably deduce that from 'man read' and I am not sure it is
reasonnable to expect from a compiler to do the same. ;-)
Imagine the function that is called returns -2. Then your 'cast'
has no sense at all.
In the initial post the construct was:
if (n < 0)
....
else
...
At the else statement, it is trivial that 'n' is never negative and
the compiler warning is quite _useless_. No need for the compiler to
be as knowledgeable as you are in order to handle this.
> Then, nr_of_bytes_read can be dealt with in whatever way desired. I know
> this is clumsy, but that is the price one pays for semantic overloading
> of the return value from read. (By the way, ()s are not required around
> sizeof's argument if it is a variable name -- only if it is a type
> name.)
My understanding of your proposal is: to use 2 variables and a signed to
unsigned cast each time we call a function that returns a signed value
that could be potentially compared to a sizeof() or some other unsigned
value. I just hope I just misunderstood your suggestion.
Gerard.