I'm dealing with some code at work that includes an expression of the form
-(sizeof(struct foo))
i.e. the negation of a size_t
, and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof
returns an unsigned integral value of type size_t
. I can't find any clear reference for specified behavior when negating an unsigned integer. Is there any, and if so, what is it?
Edit: Ok, so there are some good answers regarding arithmetic on unsigned types, but it's not clear that this is in fact such. When this negates, is it operating on an unsigned integer, or converting to a signed type and doing something with that? Is the behavior to expect from the standards "imagine it's the negative number of similar magnitude and then apply the 'overflow' rules for unsigned values"?
The only thing I can think of is so wrong it makes my head hurt...
Overflow is a feature!
Both ISO C and ISO C++ standards guarantee that unsigned arithmetic is modulo 2n - i.e., for any overflow or underflow, it "wraps around". For ISO C++, this is 3.9.1[basic.fundamental]/4:
For ISO C(99), it is 6.2.5/9:
Which means the result is guaranteed to be the same as
SIZE_MAX - (sizeof(struct foo)) + 1
.In ISO 14882:2003 5.3.1.7:
negating an unsigned number is useful for propagating the lsb across the word to form a mask for subsequent bitwise operations.
From the current C++ draft standard, section 5.3.1 sentence 8:
So the resulting expression is still unsigned and calculated as described.
User @outis mentioned this in a comment, but I'm going to put it in an answer since outis didn't. If outis comes back and answers, I'll accept that instead.
http://msdn.microsoft.com/en-us/library/wxxx8d2t%28VS.80%29.aspx
In other words, the exact behavior will be architecture-specific. If I were you, I would avoid using such a weird construct.
size_t
is an implementation-defined unsigned integer type.Negating a
size_t
value probably gives you a result of typesize_t
with the usual unsigned modulo behavior. For example, assuming thatsize_t
is 32 bits andsizeof(struct foo) == 4
, then-sizeof(struct foo) == 4294967292
, or 232-4.Except for one thing: The unary
-
operator applies the integer promotions (C) or integral promotions (C++) (they're essentially the same thing) to its operand. Ifsize_t
is at least as wide asint
, then this promotion does nothing, and the result is of typesize_t
. But ifint
is wider thansize_t
, so thatINT_MAX >= SIZE_MAX
, then the operand of-
is "promoted" fromsize_t
toint
. In that unlikely case,-sizeof(struct foo) == -4
.If you assign that value back to a
size_t
object, then it will be converted back tosize_t
, yielding theSIZE_MAX-4
value that you'd expect. But without such a conversion, you can get some surprising results.Now I've never heard of an implementation where
size_t
is narrower thanint
, so you're not likely to run into this. But here's a test case, usingunsigned short
as a stand-in for the hypothetical narrowsize_t
type, that illustrates the potential problem:The output on my system (which has 16-bit
short
, 32-bitint
, and 64-bitsize_t
) is: