19

Here is the code which prints size of different classes

#include <iostream>

using namespace std;

class EmptyClass
{    
};

class AbstractClass
{
  public: 
          virtual void funcOne() = 0;
          virtual void funcTwo() = 0;
};

class NotAbstrClass
{
  public: int virtFunc( int );
};

class MixClass
{
  public:
          virtual void clFunc( int );
          static int i;
          int j;
};

int main()
{
    // Print size of class or class objects
    cout<<"Size of empty class: "<< sizeof(EmptyClass)<<endl;          
    cout<<"Size of Abstract class :"<< sizeof(AbstractClass)<<endl;
    cout<<"Size of Non Abstract class: "<< sizeof(NotAbstrClass)<<endl;
    cout<<"Size of Mix class: "<< sizeof(MixClass)<<endl;
    return 0;
}

The output of the program on C++11 compiler is

Size of empty class: 1
Size of Abstract class :4
Size of Non Abstract class: 1
Size of Mix class: 8

I understand why Empty class has size 1 Size of empty class object. For abstract class, the object stores a pointer for implementing virtual function call mechanisms. But what about the sizes of other class objects (NotAbstrClass and MixClass) ?

Community
  • 1
  • 1
nurabha
  • 1,152
  • 3
  • 18
  • 42
  • Understand that anything sizeof() tells you is inherently dependent on that particular compiler in that particular operating system and those particular settings. – Sebastian Redl Jun 24 '13 at 18:04
  • @SebastianRedl: you are correct about it. But for the time being consider that I am using a 32-bit desktop processor architecture with a 32-bit OS. In such case you can use concrete values for size of integer types (4 bytes) and pointer type(4 byte). – nurabha Jun 24 '13 at 18:13
  • 1
    duplicate of http://stackoverflow.com/questions/4766323/how-to-determine-sizeof-class-with-virtual-functions/4766385#4766385 – kfsone Jun 24 '13 at 18:14
  • I know this is a stupid question and I thought the same when I was asked this sort of questions in an interview with a semiconductor company. – nurabha Jun 24 '13 at 18:39

3 Answers3

7

NotAbstrClass has no data members, so it too is an empty class. Since classes cannot be zero-sized, you get the same treatment as EmptyClass.

MixClass has a virtual function, and 1 non-static data member. It seems each of these (vptr and int) occupy 4 bytes on your platform, so the size is 8 bytes.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • The static data member of MixClass is part of class namespace, shared by all objects of the class but is not part of class object. Is that why it doesn't contribute to size of the MixClass object ? – nurabha Jun 24 '13 at 18:22
  • @nurabha You answered it yourself. The static data member is shared between all instances of that class, and as such it doesn't belong to any individual instance. If it's not part of any instance, how can it add to the size of one? – Praetorian Jun 24 '13 at 18:28
4

According to Girish Shetty:

There are many factors that decide the size of an object of a class in C++.

These factors are:

  • Size of all non-static data members
  • Order of data members
  • Byte alignment or byte padding
  • Size of its immediate base class
  • The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
  • Compiler being used
  • Mode of inheritance (virtual inheritance)

Here there are some related website, I think it can be helpful to you.

Determine the size of class object: http://www.cprogramming.com/tutorial/size_of_class_object.html

Memory layout: http://www.phpcompiler.org/articles/virtualinheritance.html

And, if you use MVSC, you can dump all memory layout of all class in your solution with -d1reportAllClassLayout like that:

cl -d1reportAllClassLayout main.cpp
Community
  • 1
  • 1
HappyTran
  • 503
  • 6
  • 9
  • 1
    who is girish shetty by the way ? – nurabha Jun 08 '15 at 09:20
  • 1
    Girish Shetty is the author of the first paper link that I refer above. So, I give his name for reference. :) – HappyTran Jun 08 '15 at 17:03
  • As a warning, the link of the memory layout currently goes to a website that my antivirus complains about. But seemingly the question cannot be edited. – turoni Nov 16 '21 at 12:29
1

NotAbstrClass is like an empty class when we talk about bit sizes since it has no data. MixClass has the virtual function pointer (4 bytes on a 32-bit machine) and an int (also 4 bytes).

Marius
  • 2,234
  • 16
  • 18