why class A and E's sizes are 4 instead of 1?

  • Follow


Hi,

I have #include <iostream>

using namespace std;

class A{
public:
  unsigned x : 5;
  unsigned y : 3;
};

class B
{ };

class C
{
public:
  char test;
};

class D
{
public:
  int x;
};

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;
}
}

0
Reply yuyang08 (7) 8/24/2006 9:09:38 PM

yuyang08@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.

0
Reply pj_hern (970) 8/25/2006 2:42:52 AM


On 24 Aug 2006 14:09:38 -0700 in comp.lang.c++, yuyang08@gmail.com
wrote,
>class A{
>public:
>  unsigned x : 5;
>  unsigned y : 3;
>};

unsigned = unsigned int.
compare:

class A{
public:
  unsigned char x : 5;
  unsigned char y : 3;
};

0
Reply source (985) 8/25/2006 5:51:16 AM

yuyang08@gmail.com wrote:

> Hi,
>
> I have #include <iostream>
>
> using namespace std;
>
> class A{
> public:
>   unsigned x : 5;
>   unsigned y : 3;
> };
>
> class B
> { };
>
> class C
> {
> public:
>   char test;
> };
>
> class D
> {
> public:
>   int x;
> };
>
> class E
> {
> public:
>   bool f1;
> };
>

  And the size of bool depends on the compliar.
   In Visual C++4.2, the Standard C++ header files contained a typedef
that equated bool with int. In Visual C++ 5.0 and later, bool is
implemented as a built-in type with a size of 1 byte.
   Also, In our old version of gcc, the size of the bool type was
apparently 4 bytes, but in gcc-3.2.3, it is 1 byte.

   According to section 5.3.3 of C++ standard, "the result of sizeof
applied to any other fundamental type is implementation
defined."[ISO98].

  I don't have a standard on my hand, these information is from
Internet or MSDN.

> 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;
> }
> }

0
Reply Shootingsyh 8/25/2006 7:32:46 AM

3 Replies
18 Views

(page loaded in 0.238 seconds)


Reply: