Thread: Size of a Empty class

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    13

    Size of a Empty class

    Hi,
    Can anybody explain why size of a Empty class is 1 byte?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because if the size is zero, how else could you make this [silly] example code work?
    Code:
    class c 
    {
    };
    
    c arr[10];
    c *p;
    ...
    if (p != &arr[0])  ...
    If the size is zero, then all c elements would have the same address.

    What difference does it make to you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It actually comes down to the fact that the unit in the C memory model is the char (which has sizeof 1 by definition), and that n*sizeof(anything) is the size of an array of n anything's. That computation would not work if sizeof(anything) could be zero.

    More precisely, sizeof a structure type provides the total size in bytes of the structure, including data it contains and any internal or trailing padding (which are used to ensure alignment of data in a manner that is suitable for the target machine). The one byte you are seeing means that your compiler adds one byte padding to an empty struct (the actual amount of padding depends on the implementation).

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't worry, it doesn't take up any space when used purely as a base class for another class. Look up "Empty base member optimization".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by kollurisrinu View Post
    Hi,
    Can anybody explain why size of a Empty class is 1 byte?
    Every object must have a unique address. So It Is Written In The Standard. But if an object occupies no space at all, how can it have an address? Therefore it must occupy space.

    Also, iMalc's comment about empty base optimization is pertinent. Same goes for empty objects as members of other objects.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    13
    Quote Originally Posted by brewbuck View Post
    Every object must have a unique address. So It Is Written In The Standard. But if an object occupies no space at all, how can it have an address? Therefore it must occupy space.

    Also, iMalc's comment about empty base optimization is pertinent. Same goes for empty objects as members of other objects.
    Why size of the empty structure is 0 bytes eventhough structure variable also have unique address?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't recall the standard saying that an empty class/structure (declaration at least) must take up any space. Although, when instantiating it, I suppose it would take a byte... or two.
    (Yes, I know this topic is about 2.5 years old.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. size of a class
    By sunny_master_07 in forum C++ Programming
    Replies: 10
    Last Post: 04-23-2008, 04:04 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM