Hi.
I have this class.
what will be the size of these two classes? Will the value be different if i run it on windows and linux OS?Code:class ab{
int a;
char b;
};
class cd{
char c;
int d;
};
Thanks.
Printable View
Hi.
I have this class.
what will be the size of these two classes? Will the value be different if i run it on windows and linux OS?Code:class ab{
int a;
char b;
};
class cd{
char c;
int d;
};
Thanks.
They will (probably) both be 8 bytes, but there's no guarantee. The compiler can padd or add extra bytes, and this varies from each compiler, so there's no 100% guaranteed answer. It does not depend on OS, however. Only the compilers.
Note that these classes will probably be the same size, however.
You can use sizeof to determine the size of the classes at runtime in your compiler.
At compile time, actually.Quote:
You can use sizeof to determine the size of the classes at runtime in your compiler.
That's a difficult question to answer, since "on Windows" and "on Linux" doesn't tell the whole story.
The key here is that char and int have different sizes, and different alignment criteria.
The "normal" alignment is that components in a struct or class is aligned to the size of the component, and the class itself has a padding to make the NEXT class provide the same alignment. So, by this, we can figure that the size of both of your classes, in normal case is "2 * sizeof(int)" - because the int needs to be aligned to it's own size, and the whole class needs to be a multiple of that size.
However, the only element that we can say for sure what size it is, without knowing the compiler vendor and possibly settings, is the char type (sizeof(char) is always one in ALL compilers for ALL architectures). All other types (short, int, float, long, double, long long, extended double or whatever they may be) are dependant on the compiler implementation - we can have a good guess that int is 4 bytes, but it's by no means SURE that this is the case.
--
Mats
I was more referring to that you can see it at compile time. But you're right, of course. The size is evaluated at compile time.
Why not check the size for yourself and find out?
its 8 in both cases.......
Lol, you gave me a good laugh there, CornedBee. Thanks :)