c++ - size_t(-1) and int(-1) under 64-bit system -


I wrote a test code to confirm some issues with 64-bit porting. But I was confused with the following matters:

  size_t size_neg_one = (size_t (-1)); Int int_neg_one = -1; Printf ("size_neg_one =% # zu, int_neg_one =% # x \ n", size_geg_on, Int_neg_one);   

Output result is:

  size_ neg_one = 0xffffffffffffff int_neg_one = 0xffffffff   

Obviously, size_neg_one < / Code> int_neg_one should not be equal to. But I tried if (size_neg_one == int_neg_one) and found TRUE does not match my expectation.

Can anyone explain this condition to me? thank you in advanced.

My DEV environment: GCC-4.6.3 with 64-bit Xubuntu 13.04 .

6.3.1.3 signed and unsigned integer

1 when the integer type With one value converted to another integer type, except _Bool, if the value can be presented by a new type, then it is not unchanged. # 2 Otherwise, if the new type is not unsigned, then the value can be changed by adding or subtracting the value from the maximum value repeatedly, until the value is represented in the new type range. Ho. 60) 3 Otherwise, new types have been signed and the value can not be represented in it; Either the result is implementation-defined or the implementation-defined signal is raised.

then int (32 bit 2-complement) is converted to size_t (64 bit unsigned), 2 ^ 64 By adding -1.

Just about every compiler will warn about such comparisons though.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -