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

Discussion in 'C++' started by yuyang08@gmail.com, Aug 24, 2006.

  1. Guest

    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;
    }
    }
     
    , Aug 24, 2006
    #1
    1. Advertising

  2. Salt_Peter Guest

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

    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.
     
    Salt_Peter, Aug 25, 2006
    #2
    1. Advertising

  3. David Harmon Guest

    On 24 Aug 2006 14:09:38 -0700 in comp.lang.c++,
    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;
    };
     
    David Harmon, Aug 25, 2006
    #3
  4. Shooting Guest

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

    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;
    > }
    > }
     
    Shooting, Aug 25, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Latter
    Replies:
    10
    Views:
    904
    John Latter
    Sep 24, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    985
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,237
    Smokey Grindel
    Dec 2, 2006
  4. dolphin
    Replies:
    4
    Views:
    340
    Jorgen Grahn
    Aug 25, 2007
  5. Sergio Correia
    Replies:
    7
    Views:
    322
    Ben Finney
    Sep 18, 2007
Loading...

Share This Page