vs涓繙紼嬭皟璇曪紝鎸囧畾ip錛宲ort錛?鍚屾椂鍦ㄨ繙紼嬩富鏈轟笂瑕佸悓鎰忚繙紼嬭皟璇曪紝鎺ュ彈鎸囧畾user銆傝淇濊瘉code涓巈xe鏂囦歡涓鑷淬?br />璁懼畾鏂偣錛岃瀵焏ebug杈撳叆錛屼笌棰勬兂鐨勬湁浠涔堜笉鍚屻傝瀵焍t錛宼hread stack 鎵懼埌鍑虹幇闂鐨勫嚱鏁版垨鑰呰鍙楓?br /> p4 涓殑check out, commit, diff, opened, edit錛?sync鍛戒護鍜屼綔鐢ㄣ?br />submit step錛歮erge changlist (from branch) (to branch) check diff resolve make (so make sure the code is right) submit (add comments) set the bug state in Web
vim:鐔熸倝浜嗗悇縐嶅懡浠ゃ?br /> cscope錛宑tag錛氬湪linux涓祻瑙堜唬鐮佸緢鏈夌敤錛屽彲浠ユ壘鍒癴unction definition, struct definition, calling function, called function.
]]>Signed to unsigned conversion in C - is it always safe? (from stackoverflow)http://www.shnenglu.com/chenglong7997/archive/2012/08/17/187437.htmlSnapeSnapeThu, 16 Aug 2012 18:32:00 GMThttp://www.shnenglu.com/chenglong7997/archive/2012/08/17/187437.htmlhttp://www.shnenglu.com/chenglong7997/comments/187437.htmlhttp://www.shnenglu.com/chenglong7997/archive/2012/08/17/187437.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/187437.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/187437.htmlSuppose I have the following C code.
unsignedint u =1234; int i =-5678;
unsignedint result = u + i;
What implicit conversions are going on here, and is this code safe for all values of u and i? (Safe, in the sense that even though result in this example will overflow to some huge positive number, I could cast it back to an int and get the real result.)
Answer:
Short Answer
Your i will be converted to an unsigned integer by adding UINT_MAX + 1, then the addition will be carried out with the unsigned values, resulting in a large result (depending on the values of u andi).
Long Answer
According to the C99 Standard:
6.3.1.8 Usual arithmetic conversions
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
In your case, we have one unsigned int (u) and signed int (i). Referring to (3) above, since both operands have the same rank, your i will need to be converted to an unsigned integer.
6.3.1.3 Signed and unsigned integers
When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
Now we need to refer to (2) above. Your i will be converted to an unsigned value by adding UINT_MAX + 1. So the result will depend on how UINT_MAX is defined on your implementation. It will be large, but it will not overflow, because:
6.2.5 (9)
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
Bonus: Arithmetic Conversion Semi-WTF
#include<stdio.h>
int main(void) { unsignedint plus_one =1; int minus_one =-1;
Arithmetic conversion rules can be used to get the value of UINT_MAX by initializing an unsigned value to -1, ie:
unsignedint umax =-1;// umax set to UINT_MAX
This is guaranteed to be portable regardless of the signed number representation of the system because of the conversion rules described above. See this SO question for more information: Is it safe to use -1 to set all bits to true?