Strange problem with size_t | | |
For some time now I have forced myself to use size_t instead of int
when I'm using a loop to go thru an array.
But today I found something odd:
long hex2long(const string& s)
{
string alpha="0123456789ABCDEF";
const long base=16;
long power=1, n=1;
for(int i=s.size()-1; i>=0; --i)
{
n+=power*alpha.find(s[i]);
power*=base;
}
return n;
}
This works, but if I make the change the int for size_t, the program
behavies on a very unspected way.
What's wrong with that code? When I should definitely use size_t insted
of int?
Thanks. | | | | re: Strange problem with size_t
Gaijinco wrote: Quote:
For some time now I have forced myself to use size_t instead of int
when I'm using a loop to go thru an array.
>
But today I found something odd:
>
long hex2long(const string& s)
{
string alpha="0123456789ABCDEF";
const long base=16;
long power=1, n=1;
for(int i=s.size()-1; i>=0; --i)
{
n+=power*alpha.find(s[i]);
power*=base;
}
return n;
}
>
This works, but if I make the change the int for size_t, the program
behavies on a very unspected way.
>
Which is?
--
Ian Collins. | | | | re: Strange problem with size_t
It prints an infinte amount of character until it crashes but I
supposed the exact behavior must be machine and compiler-dependant so I
did'nt want to get into details.
What's wrong with using size_t? | | | | re: Strange problem with size_t
Gaijinco wrote: Quote:
For some time now I have forced myself to use size_t instead of int
when I'm using a loop to go thru an array.
>
But today I found something odd:
>
long hex2long(const string& s)
{
string alpha="0123456789ABCDEF";
const long base=16;
long power=1, n=1;
for(int i=s.size()-1; i>=0; --i)
size_t is unsigned, so i>=0 will never fail if i is a size_t.
--
Ian Collins. | | | | re: Strange problem with size_t
Gaijinco wrote: Quote:
For some time now I have forced myself to use size_t instead of int
when I'm using a loop to go thru an array.
>
But today I found something odd:
>
long hex2long(const string& s)
{
string alpha="0123456789ABCDEF";
const long base=16;
long power=1, n=1;
for(int i=s.size()-1; i>=0; --i)
{
n+=power*alpha.find(s[i]);
power*=base;
}
return n;
}
>
This works, but if I make the change the int for size_t, the program
behavies on a very unspected way.
>
What's wrong with that code? When I should definitely use size_t insted
of int?
>
Thanks.
>
size_t is unsigned, so it will never be less than zero; hence, the
expression (i>=0) is always true. Counting down with unsigned variables
can be tricky.
You could do something like:
....
const size_t size = s.size();
for(size_t i=size-1; i<size; --i)
{
....
--
Clark S. Cox III clarkcox3@gmail.com | | | | re: Strange problem with size_t
Gaijinco wrote: Quote:
For some time now I have forced myself to use size_t instead of int
when I'm using a loop to go thru an array.
>
But today I found something odd:
>
long hex2long(const string& s)
{
string alpha="0123456789ABCDEF";
const long base=16;
long power=1, n=1;
for(int i=s.size()-1; i>=0; --i)
{
n+=power*alpha.find(s[i]);
power*=base;
}
return n;
}
>
This works, but if I make the change the int for size_t, the program
behavies on a very unspected way.
size_t is an unsigned type. The idioms for counting down are therefore
different:
for(size_t i=s.size(); i-- 0; )
{
n+=power*alpha.find(s[i]);
power*=base;
}
Best
Kai-Uwe Bux | | | | re: Strange problem with size_t
Kai-Uwe Bux posted: Quote:
for(size_t i=s.size(); i-- 0; )
{
n+=power*alpha.find(s[i]);
power*=base;
}
My own personal favourite is:
for(size_t i = WHATEVER - 1; (size_t)-1 != i; --i)
(Cast is used to suppress compiler warning)
Or another one would be:
for(size_t i = WHATEVER -1; ~i; --i)
The latter example will break if integer promotion takes place (but I don't
think it should for a size_t).
--
Frederick Gotham | | | | re: Strange problem with size_t
Frederick Gotham wrote: Quote:
Kai-Uwe Bux posted:
> Quote:
> for(size_t i=s.size(); i-- 0; )
> {
> n+=power*alpha.find(s[i]);
> power*=base;
> }
>
>
My own personal favourite is:
>
for(size_t i = WHATEVER - 1; (size_t)-1 != i; --i)
>
(Cast is used to suppress compiler warning)
>
Or another one would be:
>
for(size_t i = WHATEVER -1; ~i; --i)
>
The latter example will break if integer promotion takes place (but I
don't think it should for a size_t).
Maybe this is a good opportunity to point out a little detail in the code I
provided: the use of "i-- 0" as opposed to "i-- != 0". I tend to prefer
reusable code. The version
for ( i = upper_bound; i-- 0; ) {
some code;
}
will work for signed and unsigned types. Thus, I could even use it when the
type of i is given by a template parameter.
Best
Kai-Uwe Bux | | | | re: Strange problem with size_t
"Kai-Uwe Bux" <jkherciueh@gmx.netwrote in message
news:eccok1$2k2$1@murdoch.acc.Virginia.EDU... Quote:
Maybe this is a good opportunity to point out a little detail in the code
I Quote:
provided: the use of "i-- 0" as opposed to "i-- != 0". I tend to prefer
reusable code. The version
>
for ( i = upper_bound; i-- 0; ) {
some code;
}
>
will work for signed and unsigned types. Thus, I could even use it when
the Quote:
type of i is given by a template parameter.
I don't see how "i-- != 0" fails to have this property. (Indeed, for
unsigned types it is exactly the same.) | | | | re: Strange problem with size_t
Philip Potter wrote: Quote:
"Kai-Uwe Bux" <jkherciueh@gmx.netwrote in message
news:eccok1$2k2$1@murdoch.acc.Virginia.EDU... Quote:
>Maybe this is a good opportunity to point out a little detail in the code
I Quote:
>provided: the use of "i-- 0" as opposed to "i-- != 0". I tend to
>prefer reusable code. The version
>>
> for ( i = upper_bound; i-- 0; ) {
> some code;
> }
>>
>will work for signed and unsigned types. Thus, I could even use it when
the Quote:
>type of i is given by a template parameter.
>
I don't see how "i-- != 0" fails to have this property. (Indeed, for
unsigned types it is exactly the same.)
If upper_bound happens to be negative, the != 0 version enters an infinite
loop, whereas the 0 version just does nothing.
Best
Kai-Uwe Bux | | | | re: Strange problem with size_t
In article <4ksc0uFdj9l1U2@individual.net>,
Ian Collins <ian-news@hotmail.comwrote: Quote:
>Gaijinco wrote: Quote:
>For some time now I have forced myself to use size_t instead of int
>when I'm using a loop to go thru an array.
>>
>But today I found something odd:
>>
>long hex2long(const string& s)
>{
> string alpha="0123456789ABCDEF";
> const long base=16;
> long power=1, n=1;
> for(int i=s.size()-1; i>=0; --i)
> {
> n+=power*alpha.find(s[i]);
> power*=base;
> }
> return n;
>}
>>
>This works, but if I make the change the int for size_t, the program
>behavies on a very unspected way.
>>
>Which is?
I'll wager a quick quess that the problem is in some case size yields 0
therfore size() - 1 is negative, but size_t is unsigned.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? | | | | re: Strange problem with size_t
"Greg Comeau" <comeau@panix.comwrote... Quote:
In article <4ksc0uFdj9l1U2@individual.net>,
Ian Collins <ian-news@hotmail.comwrote: Quote:
>>Gaijinco wrote: Quote:
>>For some time now I have forced myself to use size_t instead of int
>>when I'm using a loop to go thru an array.
>>>
>>But today I found something odd:
>>>
>>long hex2long(const string& s)
>>{
>> string alpha="0123456789ABCDEF";
>> const long base=16;
>> long power=1, n=1;
>> for(int i=s.size()-1; i>=0; --i)
>> {
>> n+=power*alpha.find(s[i]);
>> power*=base;
>> }
>> return n;
>>}
>>>
>>This works, but if I make the change the int for size_t, the program
>>behavies on a very unspected way.
>>>
>>Which is?
>
I'll wager a quick quess that the problem is in some case size yields 0
therfore size() - 1 is negative, but size_t is unsigned.
"In some case"? In *all cases* the function is bound to run infinitely
if 'i' is 'size_t' because the condition in the 'for' statement (i>=0) is
simply always true (all unsigned values are greater than, or equal, zero).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: Strange problem with size_t
Greg Comeau wrote: Quote:
In article <4ksc0uFdj9l1U2@individual.net>,
Ian Collins <ian-news@hotmail.comwrote:
> Quote:
>>Gaijinco wrote:
>> Quote:
>>>For some time now I have forced myself to use size_t instead of int
>>>when I'm using a loop to go thru an array.
>>>
>>>But today I found something odd:
>>>
>>>long hex2long(const string& s)
>>>{
>> string alpha="0123456789ABCDEF";
>> const long base=16;
>> long power=1, n=1;
>> for(int i=s.size()-1; i>=0; --i)
>> {
>> n+=power*alpha.find(s[i]);
>> power*=base;
>> }
>> return n;
>>>}
>>>
>>>This works, but if I make the change the int for size_t, the program
>>>behavies on a very unspected way.
>>>
>>
>>Which is?
>
>
I'll wager a quick quess that the problem is in some case size yields 0
therfore size() - 1 is negative, but size_t is unsigned.
I was prompting the OP the explain "behavies on a very unspected way".
Posters often make vague statements like "didn't work" where an exact
description would help to identify the problem.
--
Ian Collins. | | | | re: Strange problem with size_t
In article <ecd7or$9cn$1@news.datemas.de>,
Victor Bazarov <v.Abazarov@comAcast.netwrote: Quote:
>"Greg Comeau" <comeau@panix.comwrote... Quote:
>In article <4ksc0uFdj9l1U2@individual.net>,
>Ian Collins <ian-news@hotmail.comwrote: Quote:
>>>Gaijinco wrote:
>>>For some time now I have forced myself to use size_t instead of int
>>>when I'm using a loop to go thru an array.
>>>>
>>>But today I found something odd:
>>>>
>>>long hex2long(const string& s)
>>>{
>>> string alpha="0123456789ABCDEF";
>>> const long base=16;
>>> long power=1, n=1;
>>> for(int i=s.size()-1; i>=0; --i)
>>> {
>>> n+=power*alpha.find(s[i]);
>>> power*=base;
>>> }
>>> return n;
>>>}
>>>>
>>>This works, but if I make the change the int for size_t, the program
>>>behavies on a very unspected way.
>>>>
>>>Which is?
>>
>I'll wager a quick quess that the problem is in some case size yields 0
>therfore size() - 1 is negative, but size_t is unsigned.
>
>"In some case"? In *all cases* the function is bound to run infinitely
>if 'i' is 'size_t' because the condition in the 'for' statement (i>=0) is
>simply always true (all unsigned values are greater than, or equal, zero).
Indeed. I wasn't trying to say otherwise, but -- clearly poorly -- why
size_t is not always text replaceable for int.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|