Connecting Tech Pros Worldwide Help | Site Map

size_t and int comparison

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:32 PM
tings
Guest
 
Posts: n/a
Default size_t and int comparison

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++?



  #2  
Old July 22nd, 2005, 11:32 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: size_t and int comparison


"tings" <tings668@hotmail.com> wrote in message
news:%RgEd.10483$c13.154@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> 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++?[/color]

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
[color=blue]
>
>[/color]


  #3  
Old July 22nd, 2005, 11:32 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: size_t and int comparison

"tings" <tings668@hotmail.com> wrote...[color=blue]
> 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.[/color]

3. Ignore the warning.
[color=blue]
> Which way is better in C++?[/color]

It depends on how 'i' is used later. I prefer #3 myself.

Victor


  #4  
Old July 22nd, 2005, 11:32 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: size_t and int comparison

"Mike Wahler" <mkwahler@mkwahler.net> wrote...[color=blue]
> [..]
> (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 */[/color]

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


  #5  
Old July 22nd, 2005, 11:32 PM
Siemel Naran
Guest
 
Posts: n/a
Default Re: size_t and int comparison

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
[color=blue]
> 3. Ignore the warning.[/color]

You can use #pragma to avoid the warning, in MSVC at least.


  #6  
Old July 22nd, 2005, 11:32 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: size_t and int comparison


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:F9OdnfhbNOT6A3zcRVn-pA@comcast.com...[color=blue]
> "Mike Wahler" <mkwahler@mkwahler.net> wrote...[color=green]
> > [..]
> > (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 */[/color]
>
> I personally prefer not to pollute scopes with unnecessary names,[/color]

So I'm a litterbug. :-)
[color=blue]
> so I'd write
>
> for (std::string::size_type sz = pathcmd.size(), i = 0; i < sz; i++) {[/color]

Yes, that's probably better.
[color=blue]
> ...
>
> But it often doesn't matter, probably.[/color]

Agreed.

-Mike


  #7  
Old July 22nd, 2005, 11:32 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: size_t and int comparison

> Two solutions to remove the warning:[color=blue]
>
> 1. Change the type of the variable 'i' to 'size_t'.
> 2. staic_cast i to "unsigned" type.[/color]

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.

  #8  
Old July 22nd, 2005, 11:32 PM
David Crocker
Guest
 
Posts: n/a
Default Re: size_t and int comparison

"tings" <tings668@hotmail.com> wrote in message
news:%RgEd.10483$c13.154@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> 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++?
>[/color]
(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



  #9  
Old July 22nd, 2005, 11:32 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: size_t and int comparison

tings wrote:[color=blue]
> 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++?
>
>[/color]
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...
  #10  
Old July 22nd, 2005, 11:32 PM
KTC
Guest
 
Posts: n/a
Default Re: size_t and int comparison

"David Crocker" <dcrocker@eschertech.ccoomm> for some reason
wrote:
[color=blue]
> "tings" <tings668@hotmail.com> wrote in message
> news:%RgEd.10483$c13.154@bgtnsc04-news.ops.worldnet.att.net...[color=green]
>> 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++?
>>[/color]
> (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
>
>
>[/color]

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
  #11  
Old July 22nd, 2005, 11:33 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: size_t and int comparison


"KTC" <me@here.com> wrote in message
news:Xns95DABBEE73AFktchaninfonews@217.158.240.10. ..[color=blue]
> "David Crocker" <dcrocker@eschertech.ccoomm> for some reason
> wrote:
>[color=green]
> > "tings" <tings668@hotmail.com> wrote in message
> > news:%RgEd.10483$c13.154@bgtnsc04-news.ops.worldnet.att.net...[color=darkred]
> >> 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++?
> >>[/color]
> > (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
> >
> >
> >[/color]
>
> erm, then why are you recommending casting an int??[/color]

He's recommending to cast the int to an unsigned type,
so that it can be safely compared against another unsigned
object.
[color=blue]
>Or is that a
> typo?[/color]

I don't think so.

-Mike


  #12  
Old July 22nd, 2005, 11:33 PM
Ioannis Vranos
Guest
 
Posts: n/a
Default Re: size_t and int comparison

tings wrote:
[color=blue]
> 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++?[/color]



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
  #13  
Old July 22nd, 2005, 11:33 PM
KTC
Guest
 
Posts: n/a
Default Re: size_t and int comparison

"Mike Wahler" <mkwahler@mkwahler.net> for some reason wrote:
[color=blue][color=green][color=darkred]
>> >>
>> > (2) is much better. Mixing up 'int' and 'size_t' is a sure[/color][/color][/color]
way[color=blue][color=green][color=darkred]
>> > 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[/color][/color][/color]
even[color=blue][color=green][color=darkred]
>> > on a 64-bit machine; but if you did, the code would break.
>> >
>> > David Crocker
>> >
>> >
>> >[/color]
>>
>> erm, then why are you recommending casting an int??[/color]
>
> He's recommending to cast the int to an unsigned type,
> so that it can be safely compared against another unsigned
> object.
>[color=green]
>>Or is that a
>> typo?[/color]
>
> I don't think so.
>
> -Mike
>
>[/color]

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
  #14  
Old July 22nd, 2005, 11:33 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: size_t and int comparison


"KTC" <me@here.com> wrote in message
news:Xns95DA2CEAF576Cktchaninfonews@217.158.240.23 ...[color=blue]
> "Mike Wahler" <mkwahler@mkwahler.net> for some reason wrote:
>[color=green][color=darkred]
> >> >>
> >> > (2) is much better. Mixing up 'int' and 'size_t' is a sure[/color][/color]
> way[color=green][color=darkred]
> >> > 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[/color][/color]
> even[color=green][color=darkred]
> >> > on a 64-bit machine; but if you did, the code would break.
> >> >
> >> > David Crocker
> >> >
> >> >
> >> >
> >>
> >> erm, then why are you recommending casting an int??[/color]
> >
> > He's recommending to cast the int to an unsigned type,
> > so that it can be safely compared against another unsigned
> > object.
> >[color=darkred]
> >>Or is that a
> >> typo?[/color]
> >
> > I don't think so.
> >
> > -Mike
> >
> >[/color]
>
> 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...[/color]

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


  #15  
Old July 22nd, 2005, 11:33 PM
Ioannis Vranos
Guest
 
Posts: n/a
Default Re: size_t and int comparison

KTC wrote:
[color=blue]
> 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.[/color]


Don't let those guys to confuse you. They just want to look cool. :-)


Use size_t.




--
Ioannis Vranos

http://www23.brinkster.com/noicys
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.