Why addresses of two different objects should be different?

前端 未结 5 1300
予麋鹿
予麋鹿 2021-01-12 03:41

I\'ve been reading about this stuff that size of an object should be at least 1 byte (C++: What is the size of an object of an empty class?) and what\'s wrong about having t

5条回答
  •  粉色の甜心
    2021-01-12 04:12

    C++ has (among other things) a rule that says pointers to objects compare equal if and only if the pointers refer to the same object. Under the scenario you propose, this would no longer be true.

    There's also quite a bit of code that simply assumes sizeof will also produce a strictly positive result. Jut for example, quite a bit of code uses things like:

    #define elements(array) ((sizeof(array)/sizeof(array[0]))
    

    For objects with a size of zero, this would result in 0/0, which is mathematically undefined.

    Rather than make changes everywhere else to support one corner case, it was a lot simpler to simply eliminate the corner case so it fit with existing rules.

提交回复
热议问题