-1

i am learning about C++ and object oriented programming. Have a small doubt -

class CA 
{
    void fun()
    {
        int x;
    }
};

class CB
{
int y;
};

int main()
{
    CA obj;
    CB obj1;
    cout<<sizeof(obj)<<'\t'<<sizeof(obj1);
}

When i run the above code, i get size of obj as '1' byte while size of obj1 as '4' byte. Why is that? Both the classes have integers, so size should be the same? How is the size of a class is calculated?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Shubhamizm
  • 35
  • 3
  • 4
    "Both the classes have integers" - no, they don't. The function fun() has an integer, created every time the function is called, but that is not part of the class. –  Sep 12 '19 at 16:58
  • 3
    `x` is not a part of `CA`. It doesn't exist except when you run `fun`, it doesn't have storage outside of that context. – François Andrieux Sep 12 '19 at 16:58
  • I suggest you read a good book on C++. Curated list can be found here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – SergeyA Sep 12 '19 at 17:05
  • @SergeyA. Could be a stock answer for any question on Stack Overflow: I suggest you read a good book on [tag]. – Bathsheba Sep 12 '19 at 17:05
  • 2
    @Bathsheba not really. But when the question is very broad and basic, it's hard to not see that OP misses some fundamental concepts of the language which would be hard to explain in Stack Overflow format. In particularly the difference between class data members and local variables of a function. – SergeyA Sep 12 '19 at 17:07
  • @SergeyA: This one passes my threshold of decency - the fact that the minimum size is 1 is buried quite deep. – Bathsheba Sep 12 '19 at 17:09
  • 1
    @Bathsheba I would certainly accept the question of *Why the size of the class with no members is 1 (not 0)* (obvious a duplicate, though). But in this case OP fails to see the difference between data member and local function variable... On any rate, we might have difference of opinions here. – SergeyA Sep 12 '19 at 17:10
  • @SergeyA: Yes, we probably had better leave it there; I'm off to open a bottle of Malbec. Have a nice evening! – Bathsheba Sep 12 '19 at 17:11
  • @Bathsheba cheers! – SergeyA Sep 12 '19 at 17:14

4 Answers4

3

CA doesn't have any member variables. (A non-virtual function such as fun and anything it might contain does not contribute to the size of an instance of a class.) But sizeof(CA) can't be zero because otherwise pointer arithmetic would break horribly on an array of CA objects! So your compiler picks a minimum value. 1 is allowed since that's no smaller than sizeof(char).

The size of CB is bounded by the size of an int; it could be greater than that due to padding.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • There is no reason to pad the class having a single int. – SergeyA Sep 12 '19 at 17:05
  • @SergeyA: Says who? Methinks MSVC might start doing it on Windows as x64 buses evolve and they stick with a 32 bit `int` for backward compatibility! – Bathsheba Sep 12 '19 at 17:06
  • Not sure I follow. Can you please elaborate why 32bit ints will have to be padded when they are inside a class? Apparently they would work fine with those evolved buses when they are on their own? What difference would the class make? – SergeyA Sep 12 '19 at 17:08
  • @SergeyA: I don't have access to such a compiler any more, but I *think* one of the Borland compilers with a 16 bit `int` pads it in a `struct`. Anyway, my point remains, it *could* still be greater. – Bathsheba Sep 12 '19 at 17:10
  • Borland what? :D I was never of a great opinion of their software anyways, that's why we used to call them Buglands. Their optimizers did hit me once or twice, if I remember correctly. – SergeyA Sep 12 '19 at 17:12
2

The variable x inside the function

void fun()
{
    int x;
}

is not a data member of the class. It is a local variable of the function that is not alive outside the function. The class does not have data members.

The size of a complete object (or class) can not be equal to 0. So the compiler sets it to 1.

In the second case the variable y is a data member of the class

class CB
{
int y;
};

Pay attention to that if in the second case you will declare a static data member instead of the non-static member like

class CB
{
    static int y;
};

then the static member does not constitute the size of the class or an object of the class. Static data members are defined (usually) outside classes. Within the class there is obly a declaration of the static member not its definition. SO again the program will output 1 for an object of such a class.

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

You can use CB to store a value. That's why the objects of this class have some extra size.

CB b;
b.y = 42;

You cannot use CA to store a value.

CA a;
a ... // what would you put there ?

When CA is used, the x value will live on the stack, outside the object, only while the function is called. As such it doesn't contribute to the object's size.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
0
  1. You see, the snippet you have provided in your question has two classes namely, CA and CB.
class CB
{
int y;
};

CB has a non-static data member(non-static members are used for calculating the size of an object among other factors) and hence it has been 4 bytes according to a 32-bit integer.

class CA 
{
    void fun()
    {
        int x;
    }
};

On the other hand, CA has a non-virtual member function containing a non-static member in it, memory to which memory is only allocated at the time it is called and doesn't contribute to the size of the object. It is treated as an empty class by the compiler. The 1 Byte is given by the compiler to uniquely identify objects by assigning them an address.

  1. There are many factors that decide the size of an object of a class in C++. These factors are (reference: Size of class object):
    1. Size of all non-static data members
    2. Order of data members
    3. Byte alignment or byte padding
    4. Size of its immediate base class
    5. The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
    6. Compiler being used
    7. Mode of inheritance (virtual inheritance) I hope this answers your questions if you need any more help feel free to ask.

Regards