Re: size_t and int comparison
From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 01/09/05
- Next message: Andreas Lagemann: "Re: calling virtual function results in calling function of base class ..."
- Previous message: Mike Wahler: "Re: size_t and int comparison"
- In reply to: tings: "size_t and int comparison"
- Next in thread: David Crocker: "Re: size_t and int comparison"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 9 Jan 2005 13:27:32 -0800
> Two solutions to remove the warning:
>
> 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.
- Next message: Andreas Lagemann: "Re: calling virtual function results in calling function of base class ..."
- Previous message: Mike Wahler: "Re: size_t and int comparison"
- In reply to: tings: "size_t and int comparison"
- Next in thread: David Crocker: "Re: size_t and int comparison"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|