views:

273

answers:

5

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"?

+2  A: 

http://msdn.microsoft.com/en-us/library/wxxx8d2t%28VS.80%29.aspx

Unary negation of unsigned quantities is performed by subtracting the value of the operand from 2n, where n is the number of bits in an object of the given unsigned type. (Microsoft C++ runs on processors that utilize two's-complement arithmetic. On other processors, the algorithm for negation can differ.)

In other words, the exact behavior will be architecture-specific. If I were you, I would avoid using such a weird construct.

John Millikin
Is that just the way Microsoft does it, or do they do it that way because it's implementation defined?
GMan
I did find this page when I was searching, and wished it said something about what part of the standard it relies on. It suggests this is not strictly defined, but is it "undefined", "implementation defined", unmentioned, or something else?Also, I think the text you quote is slightly erroneous. That should be subtracting from 2^n
Novelocrat
I don't have access to the C standard, but GCC also behaves this way (on x86). Sadly, I don't have access to a system which uses a non twos-complement representation for negative numbers to test on. If I remember correctly, integer representations are implementation-defined, so this would be also.
John Millikin
ISO 14882:2003 5.3.1.7 basically says the same thing, but doesn't include the caveat about non-two's complement machines.
outis
@outis: could you quote the text in question, possibly in a separate answer? That might be what I'm really getting at.
Novelocrat
why is this getting upvoted?
anon
@Novelocrat: done, but as a comment for Pavel's answer, as he's basically there already.
outis
@outis: Pavel's answer really wasn't what I needed. The standards text you found was.
Novelocrat
+17  A: 

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:

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.41

...

41) This implies that unsigned arithmetic does not 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 unsigned integer type.

For ISO C(99), it is 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.

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:

[...] The negative of an unsigned quantity is computed by subtracting its value from 2n, where n is the number of bits in the pro- moted operand. The type of the result is the type of the promoted operand.

Pavel Minaev
Add in ISO 14882:2003 5.3.1.7: "[...] The negative of an unsigned quantity is computed by subtracting its value from 2<sup>n</sup>, where <em>n</em> is the number of bits in the pro- moted operand. The type of the result is the type of the promoted operand."
outis
The latter part is what's most relevant to the question I asked, though the former part shows how it's consistent with the approach taken elsewhere in the standard.
Novelocrat
A: 

The only thing I can think of is so wrong it makes my head hurt...

size_t size_of_stuff = sizeof(stuff);

if(I want to subtract the size)
    size_of_stuff = -sizeof(stuff);

size_t total_size = size_of_stuff + other_sizes;

Overflow is a feature!

Chad Simpkins
You might be imagining a bit too narrowly. Suppose the result of the expression is assigned to an `int` or `long` - some signed type.
Novelocrat
A: 

From the current C++ draft standard, section 5.3.1 sentence 8:

The operand of the unary - operator shall have arithmetic or enumeration type and the result is the negation of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2n, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand.

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.

Novelocrat
How is this different from from what pavel posted?
anon
A: 

negating an unsigned number is useful for propagating the lsb across the word to form a mask for subsequent bitwise operations.

Ste