Why the size of empty class is 1 byte?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shubhangi24
    New Member
    • Oct 2012
    • 20

    Why the size of empty class is 1 byte?

    Why is the size of this empty class 1 byte?
    Code:
    class A
    {
      
    };
    
    sizeof(A);
    ans:- 1 byte
    Last edited by Frinavale; Sep 16 '14, 04:19 PM. Reason: Added the question posted in the title of the thread to the body of the thread so that code has a purpose. Also added code tags.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Think about it.

    Code:
    class A
    {
    
    };
    
    A obj;
    
    A* ptr = &obj;
    You can always create an object from a class. That means you can take the address of the object. For that to work you need an actual object in memory. A 1 byte memory allocation is the minimum required to acquire an address.

    Comment

    Working...