my output

C

Claudio

     printf ("\n %u " , sizeof ( ub ) ) ;
     printf ("\n %u " , sizeof ( sb ) ) ;
     printf ("\n %u " , sizeof ( n ) ) ;
     printf ("\n %u " , sizeof ( char ) ) ;

     ub.c = 'a' ;

     printf ("\n\n %c " ,  ub.c ) ;
     printf ("\n %u " ,  ub.a.high ) ;
     printf ("\n %u " ,  ub.a.low ) ;

my output :

8
8
4
1

a
97
9515004 [root@localhost home]

***************************************************

this is my output i have compiled with g++ in fedora core 2

question was : the amount of memory struct snibble use

i thinked struct of bit (snibble) ,
you can see,
get at most 4 bit in memory and not 4 bytes.


struct snibble {
unsigned a : 1 ;
unsigned b : 1 ;
unsigned c : 1 ;
unsigned d : 1 ;
} ;

tomorrow i try disassemble program and see even bits are bits or bytes !?

thx a lot

claudio
 
J

Jack Klein

     printf ("\n %u " , sizeof ( ub ) ) ;
     printf ("\n %u " , sizeof ( sb ) ) ;
     printf ("\n %u " , sizeof ( n ) ) ;
     printf ("\n %u " , sizeof ( char ) ) ;

     ub.c = 'a' ;

     printf ("\n\n %c " ,  ub.c ) ;
     printf ("\n %u " ,  ub.a.high ) ;
     printf ("\n %u " ,  ub.a.low ) ;

my output :

8
8
4
1

a
97
9515004 [root@localhost home]

***************************************************

this is my output i have compiled with g++ in fedora core 2

question was : the amount of memory struct snibble use
A bit-field structure is an object, and all objects must be at least
one byte in size. If you have an array of these structures, each one
must have its own address, therefore each one must occupy a whole byte
by itself.
i thinked struct of bit (snibble) ,
you can see,
get at most 4 bit in memory and not 4 bytes.


struct snibble {
unsigned a : 1 ;
unsigned b : 1 ;
unsigned c : 1 ;
unsigned d : 1 ;
} ;

Both the C and C++ language standards leave it up to the
implementation (the compiler) to select a type for storing bit-field
structures. It is very common for implementations to use an int or
unsigned int data type for this underlying storage type. On most 32
bit compilers, the size of this bit-field structure will be
sizeof(int), which happens to be 4 on most of these platforms.

Only 4 bits of the 32 bit integer type will actually be used, the rest
will not contain anything useful, but you are confusing the size of
the data with the size of the container.
 
G

Gottfried Eibner

printf ("\n %u " , sizeof ( ub ) ) ;
printf ("\n %u " , sizeof ( sb ) ) ;
printf ("\n %u " , sizeof ( n ) ) ;
printf ("\n %u " , sizeof ( char ) ) ;

ub.c = 'a' ;

printf ("\n\n %c " ,  ub.c ) ;
printf ("\n %u " ,  ub.a.high ) ;
printf ("\n %u " ,  ub.a.low ) ;

my output :

8
8
4
1

a
97
9515004 [root@localhost home]

***************************************************

this is my output i have compiled with g++ in fedora core 2

question was : the amount of memory struct snibble use

i thinked struct of bit (snibble) ,
you can see,
get at most 4 bit in memory and not 4 bytes.


struct snibble {
unsigned a : 1 ;
unsigned b : 1 ;
unsigned c : 1 ;
unsigned d : 1 ;
} ;

tomorrow i try disassemble program and see even bits are bits or bytes !?

thx a lot

claudio

try:
struct highnibble
{
unsigned char unusedh:4;
unsigned char d:1;
unsigned char c:1;
unsigned char b:1;
unsigned char a:1;
}

struct lownibble
{
unsigned char d:1;
unsigned char c:1;
unsigned char b:1;
unsigned char a:1;
unsigned char unusedl:4;
}

so the compiler is informed to use char instead of CPU-dependent int for
your struct.
both gcc/Linux and cl/Microsoft build a struct consuming one byte of memory.
if you make a union of both with char, you can (nearly) do what you want.
see reply on your request
'bitfield & union strange ?!' from the 8th of august.

regards,
godfired
 

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
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top