P
pandionx
Hi,
This may be a silly question, but I would appreciate some insight into
this.
I know the compiler will align a class' members. However, it is
unclear to me whether the compiler will align the classes themselves.
For example:
#include <iostream>
using namespace std;
struct sa
{
char a;
};
struct sb
{
char a;
char b;
sa c;
char d;
};
void main()
{
sb x;
long aa = (long)(&(x.a));
cout << "a " << (long)&x.a - aa << endl
<< "b " << (long)&x.b - aa << endl
<< "c " << (long)&x.c - aa << endl
<< "d " << (long)&x.d - aa << endl;
}
In the above example I get:
a 0
b 1
c 2
d 3
Now my question is, will the location of c be different on some
platforms, because b is a class? Ie. do some compilers have rules that
a class must always start on a specific boundary? Or is it related to
the first member of the class?
Thanks in advance...
This may be a silly question, but I would appreciate some insight into
this.
I know the compiler will align a class' members. However, it is
unclear to me whether the compiler will align the classes themselves.
For example:
#include <iostream>
using namespace std;
struct sa
{
char a;
};
struct sb
{
char a;
char b;
sa c;
char d;
};
void main()
{
sb x;
long aa = (long)(&(x.a));
cout << "a " << (long)&x.a - aa << endl
<< "b " << (long)&x.b - aa << endl
<< "c " << (long)&x.c - aa << endl
<< "d " << (long)&x.d - aa << endl;
}
In the above example I get:
a 0
b 1
c 2
d 3
Now my question is, will the location of c be different on some
platforms, because b is a class? Ie. do some compilers have rules that
a class must always start on a specific boundary? Or is it related to
the first member of the class?
Thanks in advance...