Why addresses of two different objects should be different?

前端 未结 5 1265
予麋鹿
予麋鹿 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.

    0 讨论(0)
  • 2021-01-12 04:14

    When you have two pointers to the same object, you can use either to manipulate the object. Result = you can access the object.

    However, if you have two objects at the same address, how would you differentiate between them in memory? If you had a pointer to that address, how would you know which object your pointer was pointing to?

    Hence the need for different addresses for each object, even if they are empty.

    0 讨论(0)
  • 2021-01-12 04:17

    Try an analogy.

    If you have two cars, and you try to have them occupy the same physical space, you'll have a crash.

    If you have two objects and you try to have them occupy the same memory space, you'll also have a crash.

    0 讨论(0)
  • 2021-01-12 04:22

    Having two objects at the same address would mean that there would be no way to distinguish between these two objects when referencing them with pointers. For example, in the following code:

    EmptyClass o1;
    EmptyClass o2;
    
    EmptyClass * po = &o;
    po->foo();
    

    Should the foo method be called on o1 or o2?

    It could be argued that since these objects have no data and no virtual methods (otherwise they would have a non-zero size), it doesn't matter on which instance the method is invoked. However, this becomes more important when we want to test if two objects are equal (i.e. if they are the same):

    template < typename T >
    bool isSame( T const & t1, T const & t2 )
    {
        return &t1 == &t2;
    }
    
    EmptyClass o1; // one object and...
    EmptyClass o2; // ...a distinct object...
    
    assert( ! isSame( o1, o2 ) ); // ...should not be one and same object!
    

    For a more concrete example, let's assume that I want to associate some objects (I could say entities) with some values, let's say in an associative container:

    Person you;
    Person me;
    
    // You and I are two different persons
    // (unless I have some dissociative identity disorder!)
    // Person is a class with entity semantics (there is only one 'me', I can't make
    // a copy of myself like I would do with integers or strings)
    
    std::map< Person *, std::string > personToName;
    
    personToName[&you] = "Andrew_Lvov";
    personToName[&me]  = "Luc Touraille";
    // Oh, bother! The program confused us to be the same person, so now you and I
    // have the same name!
    

    So yes, it all boils down to object identity: if objects were allowed to be empty, they could be deprived of their identity, which is simply not allowed by the language (thankfully).

    0 讨论(0)
  • 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?

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