473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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++?
Jul 22 '05 #1
14 15969

"tings" <ti******@hotmail.com> wrote in message
news:%R*****************@bgtnsc04-news.ops.worldnet.att.net...
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++?
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

Jul 22 '05 #2
"tings" <ti******@hotmail.com> wrote...
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.
3. Ignore the warning.
Which way is better in C++?


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

Victor
Jul 22 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote...
[..]
(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 */


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
Jul 22 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message
3. Ignore the warning.


You can use #pragma to avoid the warning, in MSVC at least.
Jul 22 '05 #5

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


So I'm a litterbug. :-)
so I'd write

for (std::string::size_type sz = pathcmd.size(), i = 0; i < sz; i++) {
Yes, that's probably better.
...

But it often doesn't matter, probably.


Agreed.

-Mike
Jul 22 '05 #6
> 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.

Jul 22 '05 #7
"tings" <ti******@hotmail.com> wrote in message
news:%R*****************@bgtnsc04-news.ops.worldnet.att.net...
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) 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

Jul 22 '05 #8
tings wrote:
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++?

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...
Jul 22 '05 #9
KTC
"David Crocker" <dc******@eschertech.ccoomm> for some reason
wrote:
"tings" <ti******@hotmail.com> wrote in message
news:%R*****************@bgtnsc04-news.ops.worldnet.att.net...
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) 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
Jul 22 '05 #10

"KTC" <me@here.com> wrote in message
news:Xn***************************@217.158.240.10. ..
"David Crocker" <dc******@eschertech.ccoomm> for some reason
wrote:
"tings" <ti******@hotmail.com> wrote in message
news:%R*****************@bgtnsc04-news.ops.worldnet.att.net...
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) 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.
Or is that a
typo?


I don't think so.

-Mike
Jul 22 '05 #11
tings wrote:
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++?


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
Jul 22 '05 #12
KTC
"Mike Wahler" <mk******@mkwahler.net> for some reason wrote:
>>
> (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.
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
Jul 22 '05 #13

"KTC" <me@here.com> wrote in message
news:Xn****************************@217.158.240.23 ...
"Mike Wahler" <mk******@mkwahler.net> for some reason wrote:
>>
> (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.
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
Jul 22 '05 #14
KTC wrote:
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
Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: William Xuuu | last post by:
Hi, This's a way of defining size_t and ssize_t in Linux: //"linux/types.h" typedef __kernel_size_t size_t; typedef __kernel_ssize_t ssize_t; //"asm/posix_types.h" typedef unsigned...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
3
by: glen stark | last post by:
Surely there is something clever to do here, besides turning off the warning? Glen
5
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct?...
10
by: kyagrd | last post by:
<code> #include <iostream> int main(void) { using namespace std; int p; int* p1 = p; int* p11 = p + 2;
24
by: Paulo Matos | last post by:
Hello, Is it safe to assume a size_t is an unsigned long? (is it forced by the standard?) Thank you, Paulo Matos
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
27
by: mike3 | last post by:
Hi. I can't believe I may have to use an array here. I've got this bignum package I was making in C++ for a fractal generator, and tried an approach that was suggested to me here a while...
3
by: Giuseppe:G: | last post by:
Hi, I apologise for starting a new thread on a partially answered question. My previous problem has gotten more serious :) So I have a function defined as follows: bool observe(const...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.