Threview Tools Sexactly how Printable Version Email this Page… Subscribe to this Thread… Search Thcheck out
Registered User Join Date Sep 2014 Posts 10

Hi all. I acquire this warning as soon as compiling a resource in c ++ with gcc in FreeBSD. Someone have the right to explain me solve?
*
Here is the entire resource to that file. I put it on pastebin bereason it has actually 7,000 lines. char.cpp - Pastebin.com Here is the warring messagge :

file.cpp: In member feature "void CHARACTER::PointChange(BYTE, int, bool, bool)": file.cpp:3088: warning: comparikid in between signed and also unsigned integer expressions


if (amount < 0 && exp < -amount) else { if (gPlayerMaxLevel <= GetLevel()) return;
Registered User Join Date Apr 2006 Posts 2,149
You need to make certain that both exp and amount are signed integer kinds (like int.) and also not size_t (which is unsigned).
It is also clear and also so it is hard to view. A dunce once searched for fire through a lighted lantern. Had he well-known what fire was, He can have cooked his rice much sooner.

You watching: C++ comparison between signed and unsigned integer expressions




Registered User
*
Join Date Feb 2013 Posts 2,665
You acquire the error because signed and also unsigned ints have the right to have actually values not representable in the other forms. It"s choose trying to compare an unsigned type to an unfavorable number, it would never before take place.


Registered User Join Date Sep 2014 Posts 10
Registered User
*
Join Date Feb 2013 Posts 2,665
Casts are a crude fix. Also, constantly remember that once you"re casting, make certain you go from positive to positive and also always from unsigned to signed. A signed will always fit into an unsigned kind assuming both are positive however the converse is not true.

laserlight View Profile View Forum Posts Visit Homeweb page
C++ Witch
*
Join Date Oct 2003 Location Singapore Posts 28,294

You gain the error because signed and unsigned ints can have worths not representable in the other types. It"s choose trying to compare an unsigned form to an unfavorable number, it would never before take place.
It is a warning, not an error. The comparisons between signed and also unsigned integer expressions are well defined. You have the right to indeed compare an expression of an unsigned integer type through an expression of a signed integer type through an adverse worth. Problem is, the outcome of the compariboy (after the usual arithmetic conversions have been performed) might not be as meant (as per mathematical intuition), so a compiler can warn about it in instance it was a mistake.
constantly remember that once you"re spreading, make certain you go from positive to positive and also always from unsigned to signed.
I do not agree: tbelow are occasions where it provides feeling to cast from signed to unsigned. The example presented by RebootS below may well be one of them (-amount is guaranteed to be positive, other than for the edge instance wright here we have a two"s enhance representation and also amount is of the the majority of negative value in the range).

A signed will certainly constantly fit into an unsigned form assuming both are positive yet the converse is not true.
Although an unfavorable worth would certainly be exterior of the variety of an unsigned integer form, the convariation to the unsigned integer type will constantly be well characterized.

I obtain probably two dozen requests for aid via some type of programming or style problem eextremely day. Many have actually even more feeling than to sfinish me hundreds of lines of code. If they execute, I ask them to discover the smallest example that exhibits the difficulty and send me that. Mostly, they then find the error themselves. "Finding the smallest routine that demonstrates the error" is a powerful debugging tool.



Registered User Join Date Apr 2006 Posts 2,149
You"re calling -amount and -exp later in the code. That"s not gonna execute what you expect even through that actors wright here it is. You really need to make both variables.
It is too clear and so it is tough to view. A dunce as soon as looked for fire via a lighted lantern. Had he well-known what fire was, He might have cooked his rice much sooner.

laserlight View Profile View Forum Posts Visit Homeweb page
C++ Witch
*
Join Date Oct 2003 Location Singapore Posts 28,294

You"re calling -amount and also -exp later on in the code. That"s not gonna do what you expect even with that actors wbelow it is.

See more: How To Dismount A Boat In Minecraft ? How Do You Get Out Of A Boat In Minecraft


It looks okay to me thanks to the checks in the if statement (various other than for the edge instance point with -amount, yet for all we recognize the code has other checks to encertain that amount never before reaches such a value). This looks obfuscated though:
I do not think we have sufficient indevelopment to be able know if arguing that both variables be of the very same type is the correct method though. Plus based upon a previous threview I suspect RebootS would be unable to understand also the effects of such a readjust just yet.

I get perhaps two dozen researches for help with some sort of programming or style difficulty eextremely day. Many have actually even more feeling than to send me thousands of lines of code. If they execute, I ask them to discover the smallest instance that exhibits the trouble and sfinish me that. Mostly, they then uncover the error themselves. "Finding the smallest program that demonstprices the error" is an effective debugging tool.



Master Apprtempt
*
Join Date Jan 2008 Posts 5,108
I perform not agree: there are occasions wbelow it provides sense to cast from signed to unsigned. The instance presented by RebootS here might well be among them (-amount is guaranteed to be positive, other than for the edge case where we have a two"s enhance depiction and also amount is of the the majority of negative worth in the range).
O_o I agree. The cast also manperiods the referenced the majority of negative value: ((~(-128))+1) + 255 + 1 == 128 However, I likewise agree with what King Mir is saying. Very probably the original poster heard advice when upon a time of the develop "Use `unsigned" kinds for worths which can not be negative." and also simply went via that advice. The poster probably doesn"t need the extra bit of width, and also the difficulty of managing eextremely signed/unsigned operation is more than likely more costly than preserving a positive `int" worth.

As much as it goes, that is actually great evidence that the negation isn"t doing what the original poster intfinished.
Soma



Registered User Join Date Apr 2006 Posts 2,149

It looks okay to me thanks to the checks in the if statement (various other than for the edge instance thing with -amount, yet for all we understand the code has actually other checks to ensure that amount never reaches such a value).

It is as well clear and also so it is difficult to check out. A dunce as soon as tried to find fire through a lighted lantern. Had he recognized what fire was, He can have actually cooked his rice a lot sooner.


Registered User Join Date Sep 2014 Posts 10



The various other variable, amount is an argument characterized as the type (signed) int. So I solved it by replacing the line:
Registered User
*
Join Date Feb 2013 Posts 2,665
I"m such a silly goose. I think I reversed the order in my post. int fits right into unsigned however the worth is just effectively stored assuming the int was >= 0. Otherwise, I"m not sure just how the value is copied if the sign bit is set.


C++まいる!Cをこわせ! Join Date Oct 2007 Location Inside my computer system Posts 24,654
If you perform a C-cast, the bits are replicated over. Hence, (uint32_t)-1 == 0xFFFFFFFF (unsigned). Though I"m not certain what the typical states. This is just experimentally showed on MSVC.

io.h absolutely IS consisted of in some contemporary compilers. It is no much longer component of the conventional for C, but it is neverthemuch less, had in the incredibly latest Pelles C versions.

You expect it"s contained as a crutch to aid prehistoric programmers limp along without them having actually to relearn as well much. Outside of your DOS world, your header file is meaningless.


Registered User
*
Join Date Feb 2013 Posts 2,665



C++まいる!Cをこわせ! Join Date Oct 2007 Location Inside my computer system Posts 24,654
No, C casts are a means of saying: compiler, I really, really, REALLY want this to be of kind T. Make it so! In other words, the compiler will certainly attempt its uttera lot of, whatever it takes, to make it feasible. It deserve to carry out a semantic translation, copy the bits (e.g. guideline casts) and also get rid of constness. That"s why it"s so dangerous.

io.h absolutely IS included in some modern-day compilers. It is no longer part of the traditional for C, however it is nevertheless, consisted of in the exceptionally latest Pelles C versions.

See more: Brand Usb 9-Pin Internal Motherboard Male Header To Usb Type-A Adaptor



You mean it"s contained as a crutch to assist primitive programmers limp along without them having actually to relearn too much. Beyond your DOS civilization, your header file is meaningmuch less.

Fast Navigation C++ Programming Top Site Areas Setups Private Messages Subscriptions Who"s Online Search Forums Forums Home Forums General Programming Boards C++ Programming C Programming C# Programming Game Programming Networking/Device Communication FAQ Board Programming Publication and also Product Reviews Platcreate Specific Boards Windows Programming Linux Programming wtbblue.com.com and AIHorizon.com"s Artificial Intelligence Boards General AI Programming Community Boards Post Discussions Tech Board General Discussions A Brief History of wtbblue.com.com Contests Board Projects and also Job Recruitment
« Previous Thread | Next Thread »
Popular pages Exactly exactly how to gain began with C++ (or C) this day C Tutorial C++ Tutorial 5 methods you deserve to learn to regime faster The 5 Most Typical Problems New Programmers Face How to put up a compiler 8 Common programming Mistakes What is C++11? Creating a game, from begin to complete Recent additions
*
How to develop a mutual library on Linux via GCC - December 30, 2011 Enum classes and nullptr in C++11 - November 27, 2011 Discover about The Hash Table - November 20, 2011 Rvalue References and also Move Semantics in C++11 - November 13, 2011 C and C++ for Java Programmers - November 5, 2011 A Gentle Overview to C++ IO Streams - October 10, 2011

Categories: Q&A