Discussion:
size_t and int comparison
(too old to reply)
tings
2005-01-09 20:54:19 UTC
Permalink
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning

warning C4018: '<' : signed/unsigned mismatch

strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.

Two solutions to remove the warning:

1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.

Which way is better in C++?
Mike Wahler
2005-01-09 21:05:59 UTC
Permalink
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
Which way is better in C++?
Change 'i' to type 'size_t'.

Or better yet, replace your char array 'pathcmd'
with a 'std::string' object, and write:

std::string pathcmd("whatever");
for(std::string::size_type i = 0; i < pathcmd.size(); ++i)
/* etc */

(if the code in your loop does not modify the string, you
might get a slight performance improvement by storing the
size before the loop and using that:

std::vector::size_type sz(pathcmd.size());
for(std::vector::size_type i = 0; i < sz; ++i)
/* etc */

-Mike
Victor Bazarov
2005-01-09 21:13:15 UTC
Permalink
Post by Mike Wahler
[..]
(if the code in your loop does not modify the string, you
might get a slight performance improvement by storing the
std::vector::size_type sz(pathcmd.size());
for(std::vector::size_type i = 0; i < sz; ++i)
/* etc */
I personally prefer not to pollute scopes with unnecessary names,
so I'd write

for (std::string::size_type sz = pathcmd.size(), i = 0; i < sz; i++) {
...

But it often doesn't matter, probably.

V
Mike Wahler
2005-01-09 21:20:16 UTC
Permalink
Post by Victor Bazarov
Post by Mike Wahler
[..]
(if the code in your loop does not modify the string, you
might get a slight performance improvement by storing the
std::vector::size_type sz(pathcmd.size());
for(std::vector::size_type i = 0; i < sz; ++i)
/* etc */
I personally prefer not to pollute scopes with unnecessary names,
So I'm a litterbug. :-)
Post by Victor Bazarov
so I'd write
for (std::string::size_type sz = pathcmd.size(), i = 0; i < sz; i++) {
Yes, that's probably better.
Post by Victor Bazarov
...
But it often doesn't matter, probably.
Agreed.

-Mike
Victor Bazarov
2005-01-09 21:09:51 UTC
Permalink
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
3. Ignore the warning.
Post by tings
Which way is better in C++?
It depends on how 'i' is used later. I prefer #3 myself.

Victor
Siemel Naran
2005-01-09 21:16:52 UTC
Permalink
Post by Victor Bazarov
3. Ignore the warning.
You can use #pragma to avoid the warning, in MSVC at least.
Jerry Coffin
2005-01-09 21:27:32 UTC
Permalink
Post by tings
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
It's giving you a warning because strlen returns a size_t, which is
some unsigned type. You're comparing it to 'i', which is a (signed)
int, and comparing a signed to an unsigned can cause rather strange
results (since each type can normally represent some values the other
can't).

It won't warn you, but you're re-computing the length of the string
every time through the loop. This makes your loop O(N * N) instead of
O(N) -- ugly unless your string is _really_ short. I'd use something
like:

for (int i=0; pathcmd[i] != '\0'; i++) {

Or, perhaps just switch to using an std::string, and while you're at
it, you might want to quit using an explicit loop and replace it with
an algorithm instead:

std::for_each(pathcmd.begin(), pathcmd.end(), do_whatever);

and possibly use boost::lambda to create do_whatever on the fly as
well...

--
Later,
Jerry.

The universe is a figment of its own imagination.
David Crocker
2005-01-09 22:03:39 UTC
Permalink
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
Which way is better in C++?
(2) is much better. Mixing up 'int' and 'size_t' is a sure way to write
non-portable code. For example, for most 64-bit C++ compilers, size_t is 64
bits whereas int is 32 bits. It's unlikely you would have a string longer
than 4Gb characters even on a 64-bit machine; but if you did, the code would
break.

David Crocker
KTC
2005-01-10 01:09:17 UTC
Permalink
Post by David Crocker
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a
warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned
type and you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
Which way is better in C++?
(2) is much better. Mixing up 'int' and 'size_t' is a sure way
to write non-portable code. For example, for most 64-bit C++
compilers, size_t is 64 bits whereas int is 32 bits. It's
unlikely you would have a string longer than 4Gb characters even
on a 64-bit machine; but if you did, the code would break.
David Crocker
erm, then why are you recommending casting an int?? Or is that a
typo?

KTC
--
Experience is a good school but the fees are high.
- Heinrich Heine
Mike Wahler
2005-01-10 02:17:34 UTC
Permalink
Post by KTC
Post by David Crocker
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a
warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned
type and you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
Which way is better in C++?
(2) is much better. Mixing up 'int' and 'size_t' is a sure way
to write non-portable code. For example, for most 64-bit C++
compilers, size_t is 64 bits whereas int is 32 bits. It's
unlikely you would have a string longer than 4Gb characters even
on a 64-bit machine; but if you did, the code would break.
David Crocker
erm, then why are you recommending casting an int??
He's recommending to cast the int to an unsigned type,
so that it can be safely compared against another unsigned
object.
Post by KTC
Or is that a
typo?
I don't think so.

-Mike
KTC
2005-01-10 04:24:56 UTC
Permalink
Post by Mike Wahler
Post by KTC
Post by David Crocker
(2) is much better. Mixing up 'int' and 'size_t' is a sure
way
Post by Mike Wahler
Post by KTC
Post by David Crocker
to write non-portable code. For example, for most 64-bit C++
compilers, size_t is 64 bits whereas int is 32 bits. It's
unlikely you would have a string longer than 4Gb characters
even
Post by Mike Wahler
Post by KTC
Post by David Crocker
on a 64-bit machine; but if you did, the code would break.
David Crocker
erm, then why are you recommending casting an int??
He's recommending to cast the int to an unsigned type,
so that it can be safely compared against another unsigned
object.
Post by KTC
Or is that a
typo?
I don't think so.
-Mike
Hmmm, okay. If one's worrying about the possible implementation's
size difference of the different types, then shouldn't one be
recommending to use the same type rather than cast? Namely, size_t
in this case...

Just wondering.

KTC
--
Experience is a good school but the fees are high.
- Heinrich Heine
Mike Wahler
2005-01-10 05:52:03 UTC
Permalink
Post by KTC
Post by Mike Wahler
Post by KTC
Post by David Crocker
(2) is much better. Mixing up 'int' and 'size_t' is a sure
way
Post by Mike Wahler
Post by KTC
Post by David Crocker
to write non-portable code. For example, for most 64-bit C++
compilers, size_t is 64 bits whereas int is 32 bits. It's
unlikely you would have a string longer than 4Gb characters
even
Post by Mike Wahler
Post by KTC
Post by David Crocker
on a 64-bit machine; but if you did, the code would break.
David Crocker
erm, then why are you recommending casting an int??
He's recommending to cast the int to an unsigned type,
so that it can be safely compared against another unsigned
object.
Post by KTC
Or is that a
typo?
I don't think so.
-Mike
Hmmm, okay. If one's worrying about the possible implementation's
size difference of the different types, then shouldn't one be
recommending to use the same type rather than cast? Namely, size_t
in this case...
Yes, that's imo the best solution. But as long as the values
being used fit in the actual type being used ('int' in this
case), the cast will do the trick.

-Mike
Ioannis Vranos
2005-01-10 06:21:22 UTC
Permalink
Post by KTC
Hmmm, okay. If one's worrying about the possible implementation's
size difference of the different types, then shouldn't one be
recommending to use the same type rather than cast? Namely, size_t
in this case...
Just wondering.
Don't let those guys to confuse you. They just want to look cool. :-)


Use size_t.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Ron Natalie
2005-01-09 22:58:51 UTC
Permalink
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
Which way is better in C++?
I'd use an unsigned (or size_t) variable for i.

However, I wouldn't write the above anyhow. You are computing
strlen(pathcmd) over and over again. Depending what you are
doing with the rest of the loop, there are better ways to count
than that...
Ioannis Vranos
2005-01-10 02:38:35 UTC
Permalink
Post by tings
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning
warning C4018: '<' : signed/unsigned mismatch
strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.
1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.
Which way is better in C++?
size_t. Since strlen() returns size_t which usually fits larger positive
integer values, why using int for i?
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Continue reading on narkive:
Loading...