Why addresses of two different objects should be different?

前端 未结 5 1268
予麋鹿
予麋鹿 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:23

    Every object must occupy distinct storage, otherwise you can't deallocate one object without deallocating others that share storage with it.

    Suppose you have two distinct objects at one address:

    Type* object1 = new Type(); //first object
    Type* object2 = new Type(); //second object
    

    and they happen to be at same addresses, then you

    delete object1;
    

    what will be deleted if they both have the same address?

提交回复
热议问题