unsigned ints as parameter to function
I have a function say like this func(unsigned int foo) { unsigned int i, retval = 0; for (i = 0; i < foo; i++) { retval++; } return retval; } 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 number) This not what I would have expected. Is this by design?
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++) { retval++; }
return retval; }
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 number)
This not what I would have expected.
Is this by design?
When you pass the -1 to the function, it will be implicit cast to an unsigned int (2^32 in this case). This is a consequence of the design of your function. When you say func(unsinged int foo) you are saying that this function only receives unsigned integers. When you pass a signed integer to it, it will break, since you are violating the signature. You must assure that foo is unsigned or change your function: func (int foo) { int i; unsigned int retval = 0; for (i = 0; i < foo; ++i) { retval++; } return retval; } Notice that you also must say that the function returns an unsigned int, or it will be implicit returning an int and similar errors might occur. []s Davi de Castro Reis
On Friday 11 February 2005 10:54 am, Davi de Castro Reis wrote:
When you pass the -1 to the function, it will be implicit cast to an unsigned int (2^32 in this case). This is a consequence of the design of your function. When you say func(unsinged int foo) you are saying that this function only receives unsigned integers. When you pass a signed integer to it, it will break, since you are violating the signature. You must assure that foo is unsigned or change your function:
right, but if I debug out the value of foo, it is still negative (-1) ah, I think I just answered my own question. When I debug the unsigned int, the debug function expects an int and so converts it back to the -1..... I understand now. Thanks B-)
On Friday 11 February 2005 10:54 am, Davi de Castro Reis wrote:
Notice that you also must say that the function returns an unsigned int, or it will be implicit returning an int and similar errors might occur.
Is there a way to ASSERT that the value is not negative? B-)
On Friday 11 February 2005 1:06 pm, Brad Bourn wrote:
On Friday 11 February 2005 10:54 am, Davi de Castro Reis wrote:
Notice that you also must say that the function returns an unsigned int, or it will be implicit returning an int and similar errors might occur.
Is there a way to ASSERT that the value is not negative? Yes and no. In your function, you are using all unsigned ints, which include the range from 0 to 2^32. In your code you can test for it: assert(i <= INT_MAX); This will assert for the range of values > INT_MAX, or 2147483648 -> 44294967295
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Thanks for the info usefull stuff! B-) On Friday 11 February 2005 11:40 am, Jerry Feldman wrote:
On Friday 11 February 2005 1:06 pm, Brad Bourn wrote:
On Friday 11 February 2005 10:54 am, Davi de Castro Reis wrote:
Notice that you also must say that the function returns an unsigned int, or it will be implicit returning an int and similar errors might occur.
Is there a way to ASSERT that the value is not negative?
Yes and no. In your function, you are using all unsigned ints, which include the range from 0 to 2^32. In your code you can test for it: assert(i <= INT_MAX); This will assert for the range of values > INT_MAX, or 2147483648 -> 44294967295
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Friday 11 February 2005 2:14 pm, Brad Bourn wrote:
Thanks for the info
usefull stuff! Of course, you could make the argument a signed integer. unsigned int func( int foo) { unsigned int i, retval = 0; assert(foo >= 0); for (i = 0; i < foo; i++) { retval++; }
return retval; } -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
I have a function say like this
func(unsigned int foo) { unsigned int i, retval = 0; for (i = 0; i < foo; i++) { retval++; }
return retval; }
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 number)
This not what I would have expected.
Is this by design? When you pass a -1 into this function, as a result of the conversion rules 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
On Friday 11 February 2005 12:44 pm, Brad Bourn wrote: 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@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
I have a function say like this
func(unsigned int foo) { unsigned int i, retval = 0; for (i = 0; i < foo; i++) { retval++; }
return retval; }
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 number)
This not what I would have expected.
Is this by design? I was thinking unsigned long in my previous response. :-) When you pass a -1 into this function, as a result of the conversion rules defined by the standard, it will pass a 0xFFFFFFFF the decimal value is 4294967295. 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
On Friday 11 February 2005 12:44 pm, Brad Bourn wrote: 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@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Brad, On Friday 11 February 2005 09:44, Brad Bourn wrote:
...
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 number)
The last time I checked, 4 - 3 is 1, not -1.
...
RRS
participants (4)
-
Brad Bourn
-
Davi de Castro Reis
-
Jerry Feldman
-
Randall R Schulz