Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

when i run my program at the for loop i receive a warning which says:

warning C4018: '<' : signed/unsigned mismatch


C++
void insertion_sort(vector<int> &items)
{
	cout<<" insertion_sort... ";
	int key = 0;
	int i=0;
	for( int j=2; j<items.size(); j++){//HERE
		key = items[j];
		i = j-1;
		while (i>0 && items[i]>key){
			items[i+1] = items[i];
			i=i-1;
		}
		items[i+1] = key;
	}
}


it seems like i cannot use vector.size() .
comparing a signed or unsigned int with a vector.size(), vector.max_size() won't work. i can't even:

C++
cout<<items.size();
//or
cout<<items.max_size();
//or
signed int a = items.size();
cout<<a;
//or
unsigned int a = items.size();
cout<<a;
//or
int a = items.size();
cout<<a;





Why does it happen?
How can i fix it?
Thanks,
Posted
Updated 26-Jul-14 23:56pm
v3
Comments
Mohibur Rashid 29-Jul-14 20:55pm    
declare your j as size_t which is a typedef of unsigned int.

That's because you declared j as an int, but items.size() returns an unsigned value. Declaring i and j as unsigned int should make it go away.
 
Share this answer
 
Comments
m.r.m.40 27-Jul-14 5:36am    
it didn't change anything:

unsigned int i=0;
for(unsigned int j=2; j
Stefan_Lang 29-Jul-14 6:36am    
items.size() returns a std::size_t, which is usally defined as a 64 bit unsigned int, not a (usually 32 bit) unsigned int. While your suggestion should work normally, it doesn't address the case where items.size() > 2^32. Of course, that may be well out of practical range :-)
Graham Breach 29-Jul-14 7:29am    
I'm sure you're right about it being a size_t in the end, but the header file has it returning a size_type member typedef. I was trying to keep my answer simple, my thinking being that "declare i and j as vector<int>::size_type instead of int" could have caused even more confusion!
items.size() returns a std::size_t, usually a 64-bit unsigned int, and j is a signed int. The compiler warns you for that, because you compare j with items.size(). Make j and i an unsigned long (an unsigned 64-bit int).
C++
unsigned long i=0;
for(unsigned long j=2; j<items.size(); j++){
 
Share this answer
 
v3
Comments
m.r.m.40 27-Jul-14 5:48am    
it didn't work.
this time without any warning or error i get a message:
assertion failed.

is there any other way that i can get the size or index of the last item of the vector ?
Thomas Daniels 27-Jul-14 5:51am    
It looks like the assertion happens in another part of your code, which we cannot see, so we cannot know the reason.
m.r.m.40 27-Jul-14 5:52am    
I'll update the question.
Thomas Daniels 27-Jul-14 5:59am    
Your updated question does not contain anything that looks like assertion; try to find where the assert function is used; this method causes the 'assertion failed'.
Stefan_Lang 29-Jul-14 6:31am    
incorrect: items.size() returns a std::size_t, which is usally defined as a 64 bit unsigned int, not a (usually 32 bit) unsigned int. There are a few systems where those two types are the same, but they are rather rare, and I wouldn't assume that the author is using one.
#pragma warning ( disable : 4018 )


or


void insertion_sort(vector<int> &items)
{
	cout<<" insertion_sort... ";
	int key = 0;
	int i=0;
	for( int j=2; j<(int)items.size(); j++){//HERE
		key = items[j];
		i = j-1;
		while (i>0 && items[i]>key){
			items[i+1] = items[i];
			i=i-1;
		}
		items[i+1] = key;
	}
}</int>
 
Share this answer
 
v2
Comments
Stefan_Lang 29-Jul-14 6:41am    
Voted a 2. Reason: both suggestion do nothing to fix the problem and pretend that everything is ok. but it isn't. Even worse, the second suggestion involves a C-style type cast in a C++ program. Please don't do that! Type casts are signs of resignation. C-style type casts casts are signs of ignorance. I suggest you read up on casting here[^].

Note that both suggestions will likely fail if your vector contains more than 2^31 items (roughly two billion). And that is exactly what the warning is about! A std::vector actually can contain that many items - in fact it can contain many more, provided you compile the program as 64 bit. (Of course, with that many items, no one would live to tell whether or not the insertion sort worked all right - but that's another issue...)
m.r.m.40 30-Jul-14 5:41am    
Well thank you very much for your great tips.
JJMatthews 29-Jul-14 11:31am    
... just giving the learning individual 2 other possible solutions (which I use every day). I'm not saying it's the best solution but why not be aware of the available options. There is absolutely nothing wrong with type casting, throwing words around like "ignorance" is ridiculous. If a person had a requirement to hold 2 billion items in an array (which hardly ever happens) than they would code accordingly.
Stefan_Lang 30-Jul-14 8:36am    
A warning is an indicator that the code you've written isn't entirely clear about some of the processing steps, or may contain an issue that may or may not cause run time problems.

While in this particular piece of code these issues will very likely never cause an actual problem, a similar piece of code may very well break: E. g. when you implement the Sieve Of Erastosthenes algorithm, you may want to use a very large bit vector, to hold flags for the prime numbers you've found. That vector may very well contain more elements than a 32 bit integer tye can count. Neither of your suggestions will prevent that program from breaking at runtime, and it will take some time just to find the cause.

With regards to type casts, if you must use them, you should use the C++ cast operators, not C-style cast.

However, the vast majority of type casts in C++ programs shouldn't be there, and wouldn't be necessary if the program would be using the correct types for its variables!

The code here is a good example: the variables i and J should be able to index any element in the vector, so they must be able to hold any non-negative number up to the maximum number of elements in a vector. int or unsigned int may or may not be sufficient, but the only way to be sure is use the types specifically provided for this, i. e. std::size_t, or std::vector::sizetype.

You can of course choose to simply suppress the warning by either of the methods you suggested, but doing so is the same as "killing the messenger": you did nothing to fix the actual problem.
JJMatthews 30-Jul-14 15:12pm    
In my 15 years writing C++ code professionally I thankfully have never had the pleasure of working with this level of nitpicking. I hold myself to much higher coding standards than has ever been required by any of my employers. Please don't discourage this young man into thinking this will be the way it is for him in his career because it is not.

let me make this simple for you:

warning != error;
warning != "problem";
warning == warning;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900