On Friday 11 February 2005 12:44 pm, Brad Bourn wrote:
I have a function say like this
func(unsigned int foo)
unsigned int i, retval = 0;
for (i = 0; i < foo; i++)
and I call it with
val = func(4 - 3);
Seems like gcc WILL pass the -1 to func, and func just get stuck in a
loop becase i starts at 0 (which is alread greater than a negative
This not what I would have expected.
Is this by design?
When you pass a -1 into this function, as a result of the
defined by the standard, it will pass a 0xFFFFFFFF (for a 32 bit system) or
0xFFFFFFFFFFFFFFFF (for a 64 bit system). In turn the decimal values are
4294967295 (for a 32 bit system) and 18446744073709551615.
The argument to a function may be an expression. In your case, this
expression will evaluate to an unsigned in. if you pass, (4 - 3), you are
passing 1. You've got to be very careful with expressions when you are
mixing signed and unsigned, especially in a 64 bit system where your int is
smaller than the long.
Jerry Feldman <gaf(a)blu.org>
Boston Linux and Unix user group
PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9