1

I have been asked a question that what will be the size of below give class:

class A
{
 void abc();
}

and now if I make that function virtual what will be the size now

class A
{
 virtual void abc();
}

Note: The question in respect to visual studio compiler.

I told that first will have the 1 byte and second will have 5 bytes as compiler adds a V pointer to the second.

I just check on visual studio 2010 in 64 bit machine:-

the size of class in first case is 1 byte and in second case 4 byte.I also do some play around and found below results which I am putting in questions:

  1. I found that if a class only have functions in it(with body or without) and no data members, the size of class always 1 byte and the object created also have the size 1 byte as below example:

    class MyClass
    {
        void abc(){int x=0;}
        int getDouble(int y){return y*2;}
    };  
    int main()
    {
        MyClass obj;;
        std::cout<<sizeof(MyClass )<<"\n";
        std::cout<<sizeof(obj)<<"\n"; 
        int x;
        std::cin>>x; 
    }
    

    output:

    1
    1
    

So in this case my question is member function do not have any size? and if so, how the compiler identify them?

  1. As I know, the size of empty class is 1 byte, but if we add a data member suppose int which has size 4 byte the class should be of 5 byte but it shows 4 byte. Same as in case of making only function to virtual that will add a v pointer which has size 4 byte, but total size of class shows 4 byte. So in this case my question is if a class itself has a size of 1 byte and we are adding any data member in it, so the final size should be 1 byte+data member size?? Or the class size will only be the size of all the data members if they present and 1 byte if they don't?? And what is the size of V pointers, is it 4 byte?

Please let me know if I am not clear.

yizzlez
  • 8,757
  • 4
  • 29
  • 44
Akash
  • 81
  • 5
  • since virtual function is present, compiler may also create virtual table to lookup at run time. size of this table needs to be also accounted. – Satish Chalasani Jul 14 '15 at 14:35
  • 1
    @SatishChalasani: A compiler might just ignore the virtual, if it can deduce, that it is never used through a base class pointer/reference to a derived class' object. Your *"will"* is a *"might"*, really. – IInspectable Jul 14 '15 at 14:37
  • @SatishChalasani virtual table would be one per class, not one per instance, so size of virtual table does not really matter, at least that's how it is done in most implementations – Slava Jul 14 '15 at 14:49

5 Answers5

5

According to the C++ Standard (9 Classes)

4 Complete objects and member subobjects of class type shall have nonzero size.109 [ Note: Class objects can be assigned, passed as arguments to functions, and returned by functions (except objects of classes for which 109) Base class subobjects are not so constrained. copying or moving has been restricted; see 12.8). Other plausible operators, such as equality comparison, can be defined by the user; see 13.5. β€”end note ]

So an empty class shall have some size. The MS VC++ sets it to 1. When a class already has a data member then its size is the size of the data member including a padding for the alignment. There is no need to add the size of an empty class to the size of added data members. It is important that the class had a non-zero size.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Objects are not allowed to have a size of 0, so empty classes may be given a size of 1. If objects had a size of 0, two unrelated objects could point to the same memory location, which would be crazy.

Non-virtual member functions don't take up any space in an object. The code for them will be compiled, placed somewhere in the binary, then calls will jump to that location. Full description of how functions are compiled, how calling conventions work, etc. are out of scope for this, but the information is all out there.

If my object suddenly requires some storage -- be it a vtable or some member data -- then I no longer need my dummy byte to satisfy object size invariants, I can use it to store meaningful data instead.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

So in this case my question is member function do not have any size?

No member functions (unless they are virtual) do not add to the class size. Check this: https://stackoverflow.com/a/24505169/1043773

As I know, the size of empty class is 1 byte, but if we add a data member suppose int which has size 4 byte the class should be of 5 byte but it shows 4 byte

No, the reason why a empty class occupies 1 byte is to handle the cases like:

A val[100];
val[n] = /*something*/; //This translates to (val+n*sizeof(A))

With sizeof(A) being 0 all elements will point to the same index which can be an address of any memory location. Basically this will be UB land. So the standard special cased empty classes to have some space. When you add some member to the class the special case doesn't applies.

Community
  • 1
  • 1
bashrc
  • 4,725
  • 1
  • 22
  • 49
0
class A
{
 void abc();
}

the size A is 1,if the size of A is 0. the complier can not tell two objects of A apart.so the complier alloc one byte for every object,in this way,every object has different address.


class A
{
 virtual void abc();
}

every A object has a virtal table pointer which takes four bytes,there is no need alloc one more byte, so its size is 4

kiviak
  • 1,083
  • 9
  • 10
0

how the compiler identify them

The internal address of the method become known at linking time. Compiler and linker identifys methods by decorated names. After linking the internal address is recorded as an argument of instruction or, in the case of inlined methods, does not defined and is not used at all. See more details on compiling and linking at C++ and the linker.

a size of 1 byte

This size is used for special cases, mostly in order to the operator new and memory allocation functions, especially for arrays, work propely.

Aleksey F.
  • 751
  • 7
  • 19