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. 13 1319
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?
--
Ian Collins.
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?
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)
size_t is unsigned, so i>=0 will never fail if i is a size_t.
--
Ian Collins.
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.
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 cl*******@gmail.com
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.
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
Kai-Uwe Bux posted:
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
Frederick Gotham wrote:
Kai-Uwe Bux posted:
> 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
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ec**********@murdoch.acc.Virginia.EDU...
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.
I don't see how "i-- != 0" fails to have this property. (Indeed, for
unsigned types it is exactly the same.)
Philip Potter wrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ec**********@murdoch.acc.Virginia.EDU...
>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.
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
In article <4k************@individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>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.
--
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?
"Greg Comeau" <co****@panix.comwrote...
In article <4k************@individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>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).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Greg Comeau wrote:
In article <4k************@individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>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.
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.
In article <ec**********@news.datemas.de>,
Victor Bazarov <v.********@comAcast.netwrote:
>"Greg Comeau" <co****@panix.comwrote...
>In article <4k************@individual.net>, Ian Collins <ia******@hotmail.comwrote:
>>>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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Prawit Chaivong |
last post by:
Hi, gurus
I've overloaded new operator. and I found that size is strange.
It's more than sizeof(myClass) 4 bytes.
Where does it come from.
I don't know the other compilers do the same thing....
|
by: leonecla |
last post by:
Hi everybody,
I'm facing a very very strange problem with a very very simple C
program...
My goal should be to write to a binary file some numbers (integers),
each one represented as a sequence...
|
by: cpptutor2000 |
last post by:
I am compiling and running the following code snippet on a Linux box -
I am really
puzzled by the answers. Could someone please tell me what might be
wrong?
void test(){
int m = 0;
int n = 0;...
|
by: asdf |
last post by:
I got a warning from the following statement:
fprintf(point_file, "CONTOUR\nCLOSE\n%d\n", curve.size());
warning: format '%d' expects type 'int', but argument 3 has type
'size_t'
should I...
|
by: kyagrd |
last post by:
<code>
#include <iostream>
int main(void)
{
using namespace std;
int p;
int* p1 = p;
int* p11 = p + 2;
|
by: arnuld |
last post by:
sizeof() operator gives 2 different types of size outputs :\ but I do not
understand why:
#include <stdio.h>
#include <stdlib.h>
int my_size( char );
|
by: KellyB |
last post by:
#include <stdio.h>
#include <limits.h>
void printbin(void* num) {
size_t i, j;
unsigned char *pc = (unsigned char* )#
pc += sizeof(num) - 1;
for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |