yu******@gmail.com wrote:
Hi,
I have #include <iostream>
using namespace std;
class A{
public:
unsigned x : 5;
unsigned y : 3;
};
...
class E
{
public:
bool f1;
};
int main()
{
cout<<"sizeof(bool)="<<sizeof(bool)<<endl;
cout<<"sizeof(A) = "<< sizeof(A) << endl;
cout<<"sizeof(B) = "<<sizeof(B) <<endl;
cout<<"sizeof(C) = "<<sizeof(C) <<endl;
cout<<"sizeof(D) = "<<sizeof(D) <<endl;
cout<<"sizeof(E) = "<< sizeof(E) <<endl;
return;
}
}
How do you figure class A and class E could ever be a size of 1?
Haven't you noticed that even an empty class is not really empty?
If it was, how would the program know which instance is which?
A programmer knows that an integer or a pointer, for example, is not
neccessarily 4 bytes.
He also knows that a class will have its members padded by the compiler
appropriately in order to save clock cycles. Are you not using a 32 bit
or 64 bit machine?
Isn't such a machine set up to support quick, efficient indexing of
their respective memory architecture schemes? Doesn't it make sense
that such an architecture would require extra steps to extract a subset
of the contents of a particular indexed address (ie: a single byte at
bits 8 to 15 of a 32 bit location)? That would slow your computer to a
crawl, wouldn't it?
A programmer's goal is to write code that is transparent to the
hardware/platform its running on. The programmer doesn't care how the
padding is implemented on one compiler/platform or another. If he does
care, he'll likely write buggy code.