This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: -sizeof()


Jan RingoÅ <tringi@mx-3.cz> writes:

> Hello, I am not a C++ guru, but this thing looks weird.
> I have following two codes:
>
> __int64 a = -4;
> printf ("%016I64x\n", a); // prints fffffffffffffffc

In this case, the 4 has type signed int, so the unary minus operator
changes it to -4 (still with type signed int).  Assignment to a then
sign-extends.

> __int64 b = -sizeof(int);
> printf ("%016I64x\n", b); // prints 00000000fffffffc

In this case, the sizeof(int) evaluates to 4 - but with type unsigned
int.  The unary minus operator changes that to the *positive* value
0xfffffffc, still with type unsigned int.  Assignment to b then
zero-extends.

You would observe the same behavior from

__int64 c = -4u;

zw


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]