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

后端 未结 6 453
[愿得一人]
[愿得一人] 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条回答
  •  萌比男神i
    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.

提交回复
热议问题