CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2000
    Location
    Seattle , WA
    Posts
    87

    Question Class X {} sizeof(X) // = 1 ?

    Class X
    {

    }

    Class Y
    {
    char c;
    }

    class Z
    {
    int i;
    }

    Now:

    sizeof(X) // == 1
    sizeof(Y) // == 1
    sizeof(Z) // == 4

    Why is the size of class X is 1 when it is empty ?
    I understand that sizeof(Y) & sizeof(Z) to be 1 and 4 because of the member variables.

    Can someone enlighten me on this.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Because no object/type can have zero size (it's in the standard, I think).

    However:
    Code:
    class X {};
    
    class Y : public X
    {
        char c;
    };
    
    // sizeof(Y) == 1, so it can contribute zero size to another object/type
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Mar 2000
    Location
    Seattle , WA
    Posts
    87
    Where can I find the C++ Standard , any site or any book.


    Thanks.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    http://www.techstreet.com/ncitsgate.html

    $18 for the PDF version


    From Secion 1.8 : The C++ Memory Model ...

    2) Objects can contain other objects, called sub-objects.
    A sub-object can be a member sub-object (9.2), a base class
    sub-object (clause 10), or an array element. An object that is
    not a sub-object of any other object is called a complete object.

    [from this class X is a complete object]


    4) If a complete object, a data member (9.2), or an array element
    is of class type, its type is considered the most derived class,
    to distinguish it from the class type of any base class subobject;
    an object of a most derived class type is called a most derived object.

    [from this class X is a most derived object]

    5) Unless it is a bit-field (9.6), a most derived object shall have
    a non-zero size and shall occupy one or more bytes of storage. Base
    class sub-objects may have zero size. An object of POD 4) type (3.9)
    shall occupy contiguous bytes of storage.

    [so Class X must occupy at least 1 byte of storage]

  5. #5
    Join Date
    Jun 1999
    Location
    San Diego, CA
    Posts
    600
    Some where in the C++ standard it is specified that when a class is empty, its size is defined as 1 rather than zero. The reason for that is facilitate object counting in object arrays.

    Why? The number of objects in an array generally can be calculated by take the offset (difference) between the point which points to just one element past the last one, minus the pointer to the first element, and then be divided by the size of the object. Obviously if the object size is zero, you can not divide by zero.

    In my opinion such specification in the C++ standard is meaningless. If an object has no none-static member, then there is no difference between one instance of it and another instance. Henceful it is meaningless to have an array of such object. So, the need for a none-zero object size NEVER occurs.

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Code:
    #include <iostream>
    using namespace std;
    
    struct X
    {
        void f()
        {
            cout << "Hello World" << endl;
        }
    };
    
    struct Y
    {
        X x_;
        int i_;
    };
    
    struct Z : X
    {
        int i_;
    };
    
    int main()
    {
        cout << "sizeof(X) = " << sizeof(X) << endl;
        cout << "sizeof(Y) = " << sizeof(Y) << endl;
        cout << "sizeof(Z) = " << sizeof(Z) << endl;
        return 0;
    }
    The above code generates the output:

    sizeof(X) = 1
    sizeof(Y) = 8
    sizeof(Z) = 4

    This generates the same output if I make X a completely empty structure. So, X with member functions but no data is a (potentially) useful class of zero size.

    Now, consider this (given the above declarations):
    Code:
    X ax[10];
    int n = sizeof(ax)/sizeof(ax[0]);
    what's the value of n, if sizeof(ax[0]) returns 0?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    The point being that, when you're writing a standard, you can't just cater for what what might be sensible. You have to make allowance for all the dumb things people might (and probably will) do. Since an array of empty structures is a possibility, and further, someone, somewhere, will try to divide by the size of that structure (irrespective of how pointless that may be) the standard has to cater for it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Mar 2000
    Location
    Seattle , WA
    Posts
    87
    Thanks to all for replying to the post. The information provided was very useful.

  9. #9
    Join Date
    Jun 2002
    Location
    Sweden
    Posts
    81
    The reason is that a zero-sized object can't have an address, so all objects must have size > 0 (and thus an address).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured