Hi Guys,
Can you please enlighten me, why the following output is this way?
I thought A class size is the summation of the byte size of its member variables.
My machine is a 64 bit machine.
#include <iostream>
using namespace std;
class A{
public:
char y;
int x;
};
int main() {
A a;
cout << sizeof(a.x) << endl; //Prints 4, understandable, it is an int
cout << sizeof(a.y) << endl; // Prints 1, ok, its a character
cout << sizeof(a) << endl; //Prints 8 !!! why is this 8, has it actually alllocated 4 byte for the charecter and using just 1, then other 3 byte is wasted. Isn't it?
return 0;
}
Why the above class size is showing 8?
Following code is priting 1 and 1 for the charecter:
class A{
public:
char y;
};
int main() {
A a;
cout << sizeof(a) << endl; //Prints 1
cout << sizeof(a.y) << endl; // Prints 1
return 0;
}
Thanks in advance,
S.