How does sizeof work in C++?

Shervan360 1,481 Reputation points
2022-04-30T23:06:36.457+00:00

Hello,

I read:

The memory space is allocated to the data members of a class only when an object of the class is declared, and not when the data members are declared inside the class.

>

Why does sizeof(Base) 44?

I didn't create an instance of the Base class and expected the compiler doesn't allocate memory for Base.

Please see the following code:

#include <iostream>
using namespace std;

class Base
{
public:
    void Print() {
        cout << "In Base..";
    }
    int a;
    int arr[10];
};


int main() {

    cout << sizeof(Base) << endl; // 44


    return 0;
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
{count} votes

0 additional answers

Sort by: Most helpful