#include <iostream>
using namespace std;
class Empty
{};
class Derived : virtual public Empty
{
char c;
};
int main()
{
cout << "sizeof(Empty) " << sizeof(Empty) << endl;
cout << "sizeof(Derived) " << sizeof(Derived) << endl;
return 0;
}
Why size is coming 8 I think it should be 9, If I declare 'c' as integer then also its coming 8 Cany you please explain me the logic
If compiled with the Microsoft C++ compiler, the size of DerivedClass is 16 bytes. If compiled with gcc (either c++ or g++), size of DerivedClass is 12 bytes.
In C++, the Size of an empty structure/class is one byte as to call a function at least empty structure/class should have some size (minimum 1 byte is required ) i.e. one byte to make them distinguishable.
Size of Empty Class is = 1. Size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses. See the following example.
The size of object of a class depends on the no. of bytes occupied by the data members of the class. }; The object of class student will occupy space of 8 bytes.
The size is Implementation dependent and it depends on how the particular compiler implementation implements virtualism & padding. You shouldn't expect the value specifically to be something. If you want to calculate the size in your program just use sizeof
and thats about it.
The size is dependent on implementation. Four bytes will be taken for the vtable
, one byte is taken for the char
, three bytes is for padding
. That makes it 8 bytes. So it depends how your compiler is implementing the virtualism and padding. You can use sizeof if you want to calculate the size of your program
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With