This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: -sizeof()
- From: Zack Weinberg <zack at codesourcery dot com>
- To: Jan RingoÅ <tringi at mx-3 dot cz>
- Cc: <gcc at gnu dot org>
- Date: Wed, 17 Nov 2004 17:17:50 -0800
- Subject: Re: -sizeof()
- References: <000701c4cd08$ee9bd900$1f209453@merovingian>
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