What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

后端 未结 6 436
[愿得一人]
[愿得一人] 2020-12-06 17:39

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

相关标签:
6条回答
  • 2020-12-06 17:42

    size_t is an implementation-defined unsigned integer type.

    Negating a size_t value probably gives you a result of type size_t with the usual unsigned modulo behavior. For example, assuming that size_t is 32 bits and sizeof(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. If size_t is at least as wide as int, then this promotion does nothing, and the result is of type size_t. But if int is wider than size_t, so that INT_MAX >= SIZE_MAX, then the operand of - is "promoted" from size_t to int. 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 to size_t, yielding the SIZE_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 than int, so you're not likely to run into this. But here's a test case, using unsigned short as a stand-in for the hypothetical narrow size_t type, that illustrates the potential problem:

    #include <iostream>
    int main() {
        typedef unsigned short tiny_size_t;
        struct foo { char data[4]; };
        tiny_size_t sizeof_foo = sizeof (foo);
        std::cout << "sizeof (foo) = " << sizeof (foo) << "\n";
        std::cout << "-sizeof (foo) = " << -sizeof (foo) << "\n";
        std::cout << "sizeof_foo = " << sizeof_foo << "\n";
        std::cout << "-sizeof_foo = " << -sizeof_foo << "\n";
    }
    

    The output on my system (which has 16-bit short, 32-bit int, and 64-bit size_t) is:

    sizeof (foo) = 4
    -sizeof (foo) = 18446744073709551612
    sizeof_foo = 4
    -sizeof_foo = -4
    
    0 讨论(0)
  • 2020-12-06 17:47

    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.

    0 讨论(0)
  • 2020-12-06 17:53

    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!

    0 讨论(0)
  • 2020-12-06 18:03

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

    0 讨论(0)
  • 2020-12-06 18:05

    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.

    0 讨论(0)
  • 2020-12-06 18:05

    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.

    0 讨论(0)
提交回复
热议问题