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

Y

yuyang08

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

Salt_Peter

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.
 
D

David Harmon

On 24 Aug 2006 14:09:38 -0700 in comp.lang.c++, (e-mail address 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;
};
 
S

Shooting

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top