By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

my output

P: n/a
¬*¬*¬*¬*¬*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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 02 Aug 2004 18:01:24 GMT, Claudio <da************@libero.it>
wrote in comp.lang.c++:
*****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.
tomorrow i try disassemble program and see even bits are bits or bytes !?

thx a lot

claudio


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

P: n/a
> 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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.