Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why class A and E's sizes are 4 instead of 1?

Reply
Thread Tools

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

 
 
yuyang08@gmail.com
Guest
Posts: n/a
 
      08-24-2006
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;
}
}

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      08-25-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      08-25-2006
On 24 Aug 2006 14:09:38 -0700 in comp.lang.c++, (E-Mail Removed)
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;
};

 
Reply With Quote
 
Shooting
Guest
Posts: n/a
 
      08-25-2006

(E-Mail Removed) 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;
> }
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:32 AM
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:31 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
The File Sizes of Pictures on my CDs Increased to Unreadable Sizes Marful Computer Support 11 03-08-2006 07:13 PM



Advertisments